How to replace a line that starts with a word (and use that old line on the new line)

I have a file with many lines and I would like to replace specific lines that start with a new line but include the old line in it. See below.

for example, if a line starts with (xyz is different for every line)

"#EXT-1,xyz"

I would like to have a line like this

!group=12, "#EXT-1,xyz", name="#EXT-1,xyz"

Is this possible to do with sed, and if so, how?

3

1 Answer

There are a number of ways of formulating it - one would be

sed '/^\"#EXT-1,.*\"/ s//!group=12, &, name=&/' file

If you want to modify the file in place, then add the -i or --in-place switch

sed -i '/^\"#EXT-1,.*\"/ s//!group=12, &, name=&/' file
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like