Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add prefix to words between demarkers after encountering a certain pattern

I am new to sed and I am trying to figure out if it is possible to add a prefix to all words between two markers that are encountered after a string match in a line

suppose I have a line

abc xyz PAT1 { PAT2 PAT3 PAT4 } cdf fgd

PAT1 is the string match and the markers being { and } in this case

I want the result to be

abc xyz PAT1 { prefix_PAT2 _prefix_PAT3 prefix_PAT4 } cdf fgd

I can write a C code to do this, but I am new to scripting, so I was wondering if it is possible in sed ?

I know how to add prefix to certain columns of a line if a string is encountered but PAT2 PAT3 PAT4 can be any column and there may be more than 3 of them

like image 514
user2809853 Avatar asked Nov 21 '25 21:11

user2809853


1 Answers

Input file contain:

abc xyz PAT1 { PAT2 PAT3 PAT4 } cdf fg

Try below code:

sed -i -e 's/PAT*/prefix_PAT/2g' filename

it will update your input file (filename) output:

abc xyz PAT1 { prefix_PAT2 prefix_PAT3 prefix_PAT4 } cdf fgd
like image 71
Abhis Avatar answered Nov 24 '25 11:11

Abhis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!