Asked  7 Months ago    Answers:  5   Viewed   38 times

I am trying to find out if it is possible to edit a file in a single sed command without manually streaming the edited content into a new file and then renaming the new file to the original file name. I tried the -i option but my Solaris system said that -i is an illegal option. Is there a different way?



The -i option streams the edited content into a new file and then renames it behind the scenes, anyway.





on macOS.

Tuesday, June 1, 2021
answered 7 Months ago

I'll have a go at this.

To delete 5 lines after a pattern (including the line with the pattern):

sed -e '/pattern/,+5d' file.txt

To delete 5 lines after a pattern (excluding the line with the pattern):

sed -e '/pattern/{n;N;N;N;N;d}' file.txt
Wednesday, June 2, 2021
answered 7 Months ago

OS X sed handles the -i argument differently to the Linux version.

You can generate a command that might "work" for both by adding -e in this way:

#      vv
sed -i -e 's|(.*).o:|$(OBJ_DIR)/1.o $(OBJ_DIR)/1.d $(TEST_OBJ_DIR)/1_utest.o:|' $@

OS X sed -i interprets the next thing after the -i as a file extension for a backup copy of the in-place edit. (The Linux version only does this if there is no space between the -i and the extension.) Obviously a side affect of using this is that you will get a backup file with -e as an extension, which you may not want. Please refer to other answers to this question for more details, and cleaner approaches that can be used instead.

The behaviour you see is because OS X sed consumes the s||| as the extension (!) then interprets the next argument as a command - in this case it begins with t, which sed recognizes as a branch-to-label command expecting the target label as an argument - hence the error you see.

If you create a file test you can reproduce the error:

$ sed -i 's|x|y|' test
sed: 1: "test": undefined label 'est'
Thursday, June 3, 2021
answered 6 Months ago

You'll need to replicate -i's behavior yourself by storing the results in a temp file and then replacing the original file with the temp file. This may seem inelegant but that's all sed -i is doing under the covers.

sed '$ s/OLD/NEW/g' test > test.tmp && cat test.tmp > test && rm test.tmp

If you care you could make it a bit more robust by using mktemp:

tmp=$(mktemp test.XXXXXX)
sed '$ s/OLD/NEW/g' test > "$tmp" && cat "$tmp" > test && rm "$tmp"
Wednesday, June 9, 2021
answered 6 Months ago

You should put the form inside each node and use ng-show and ng-hide to enable and disable editing, respectively. Something like this:

  <span ng-hide="editing" ng-click="editing = true">{{}} | {{}}</span>
  <form ng-show="editing" ng-submit="editing = false">
    <input type="text" ng-model="" placeholder="Name" ng-required/>
    <input type="date" ng-model="" placeholder="Date" ng-required/>
    <button class="btn" type="submit">Save</button>

The key points here are:

  • I've changed controls ng-model to the local scope
  • Added ng-show to form so we can show it while editing
  • Added a span with a ng-hide to hide the content while editing
  • Added a ng-click, that could be in any other element, that toggles editing to true
  • Changed ng-submit to toggle editing to false

Here is your updated Plunker.

Friday, July 9, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :