user419534
user419534

Reputation: 619

modify a line using sed

I need to used sed for following requirment using sed

I have one string as $str and I need to replace blow line in a file

abh{1..$abh}             cdf_$ghu,xyz                 *  abh{}.$xy

New modified line should be as below

abh{1..$abh}             cdf_$ghu,$str                 *  abh{}.$xy

Note "xyz" can be any arbitrary value. Could you please tell me how to do using sed in one liner.

sed 's/\(^\s*abh{1..$abh}\s*\)\(.*xyz\)/\1/' file.txt

but still does not work. Any help would be appreciated.

Upvotes: 1

Views: 404

Answers (2)

Vishal
Vishal

Reputation: 283

echo 'abh{1..$abh}             cdf_$ghu,xyz                 *  abh{}.$xy' | sed 's/\(.*\$ghu,\)\(.*\)\( .*\)/\1\$str\3/g'

Upvotes: 0

Slava Semushin
Slava Semushin

Reputation: 15204

Try this:

$ sed 's|\(\S\+\s\+[^,]\+,\)\S\+\(\s\+.*\)|\1$str\2|' file.txt 
abh{1..$abh}             cdf_$ghu,$str                 *  abh{}.$xy

Or even more simple:

$ sed 's|,\S\+|,$str|' example.txt

Upvotes: 1

Related Questions