Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 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.

share|improve this answer
    
any other way to accomplish this task ? –  stasunny Jan 12 '12 at 9:09
add comment

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.

share|improve this answer
    
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
    
thanks guys... it worked perfectly –  stasunny Jan 18 '12 at 2:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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