Question

I want to add specific string at the end on each line that has some Strings and the blank line should stay unaffected. How can I use the sed command so that the string i require is appened after only selected lines.

Say file name be temp.txt

abc "123.cf"
abc
abc "456.cf"
ksh
entry "e123.cf"

Now i want to append > /dev/null 2>&1 & to the end of each line that has .cf" in the end and need the result to be

abc "123.cf" > /dev/null 2>&1 &
abc
abc "456.cf" > /dev/null 2>&1 &
ksh
entry "e123.cf" > /dev/null 2>&1 &

I'm using the command:

sed -e 's:.cf"$:.ccf" > /dev/null 2>&1 &:' -i temp.txt

Does'nt seem to work. :-( Please help.

Was it helpful?

Solution

awk '/.cf"$/ {$(NF+1) = "> /dev/null 2>&1 &"} 1'

OTHER TIPS

kent$  sed 's#.cf"$#& > /dev/null 2>\&1 \&#' file
abc "123.cf" > /dev/null 2>&1 &
abc
abc "456.cf" > /dev/null 2>&1 &
ksh
entry "e123.cf" > /dev/null 2>&1 &

it worked in bash.

A variation of Glenns post:

awk '/.cf"$/ {$0=$0FS"> /dev/null 2>&1 &"}8' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top