3

I want to append #SB# at the beginning of every line where it matches a given string

incron.sh

sed -i -e'/test1/s/^/#SB#/g' file1
sed -i -e'/test2/s/^/#SB#/g' file1
sed -i -e'/test4/s/^/#SB#/g' file1

file1

/apps/pmserver $ cat file1
00 45 /ser/bat/ab.sh test1
00 45 /ser/bat/ab.sh test2
00 45 /ser/bat/ab.sh test3
00 45 /ser/bat/ab.sh test4
/apps/pmserver $

I am using Korn Shell.

I can output to a new file but it doesn't work if I have more than one sed statement in a script as shown above. I am getting error

sed: Not a recognized flag: i

What I am trying to do is comment out a few crontab entries belonging to particular team. test1 test2... are their file names and these need to be turned off.

2

The -i argument to sed is a GNU extension. You are likely not running GNU sed, which is why you're getting the error about -i being unrecognized.

  • any other way to accomplish this task ? – stasunny Jan 12 '12 at 9:09
1

Your sed is non-GNU (probably) so -i for in place editing is not supported. You can avoid it by using a temporary file:

sed -i -e'/test1/s/^/#SB#/g' file1

becomes

sed -e'/test1/s/^/#SB#/g' file1 > temp_file
mv temp_file file1

Of course you can do it better by using mktemp like this

tmpfile=`mktemp`; sed -e '/test1/s/^/#SB#/g' file1 > $tmpfile; mv $tmpfile file1

Absolutely to avoid sed and redirect without temporary file as your original file will get truncated.

  • This works for single Sed statement.I have multiple sed statments. '>' will overwrite the previous sed statement commit test1,test2,test3 are the search strings. I want to comment out any line that contains these strings. sed -i -e'/test1/s/^/#SB#/g' file1 sed -i -e'/test2/s/^/#SB#/g' file1 sed -i -e'/test4/s/^/#SB#/g' file1 – stasunny Jan 12 '12 at 13:17
  • You can make one statment in your case with sed -e'/test1/s/^/#SB#/g' -e'/test2/s/^/#SB#/g' -e'/test4/s/^/#SB#/g' file1 or even just write them all sed -e'/test[124]/s/^/#SB#/g' file1. But use carefully this construction. Error in sed statement will spoil your file. – rush Jan 12 '12 at 14:25
  • @stasunny: forget the -i option, it doesn't work. I wrote how to comment lines having the test1 pattern, but you can always apply the same instructions many times, changing the test pattern each time. A better solution would use multiple -e options like Rush showed you, always remembering to write to a temporary file. Even better IMHO would be writing a sed script containing all the lines and telling sed to use that file as script. – David Costa Jan 12 '12 at 16:32
0

You can use Vim in Ex mode:

ex -sc 'g/\vtest(1|2|4)/s/^/#SB#/' -cx file1
  1. g global regex

  2. \v turn on magic

  3. s substitute

  4. x save and close

0

The answers explain, that -i is non standard, one even explains to use a temporary file instead, but they ignore the main question how to do it for multiple statements.

You could of course redirect each statement and copy back after each statement. But you don't need separate sed calls for each statement anyhow, you can specify several scripts with several -e:

sed -e '/test1/s/^/#SB#/g' -e '/test2/s/^/#SB#/g' -e' /test4/s/^/#SB#/g' file1

Or you can concatenate sed commands with ;:

sed '/test1/s/^/#SB#/g;/test2/s/^/#SB#/g;/test4/s/^/#SB#/g' file1

In your example you can even find a regular expression to combine the replacements:

sed '/test[124]/s/^/#SB#/g' file1

Ans once we are improving the script: The g flag to the s commands stands for "substitute all occurences on the line", but as each line has only one beginning (^), you can drop it, so your final job is

sed '/test[124]/s/^/#SB#/' file1 > /tmp/file1 && mv /tmp/file1 file1

(The && is used to avoid destroying you file when sed fails)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.