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.

inputs:

line1 with the PATTERN that contains ( ) 
line2 with the PATTERN that contains ( ) 
lineN with the PATTERN that contains ( ) 

outputs:

line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;

I tried this:

find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"

but it didn't work.

share|improve this question

4 Answers 4

The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.

Also, you don't need xargs here, it's safer to use the -exec flag of fine:

find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '{}' +

If your version of find doesn't work with + at the end, then use \; instead (thank you @glenn-jackman):

find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '{}' \;

Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.

share|improve this answer
1  
You'll gain some efficiency with -exec ... + instead of -exec ... \;, if your find allows it. –  glenn jackman Feb 28 at 22:36
    
Thanks @glennjackman, updated my answer –  janos Feb 28 at 22:39
perl -ipe 's/$/;/ if /PATTERN/'

This will add a ; at the end if the line contains PATTERN.

share|improve this answer

Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:

sed -i '/(.*)/ s/$/ ;/' test.txt
share|improve this answer

Try:

sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
share|improve this answer

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.