简体   繁体   中英

How to append a character between two characters in Bash

I have a list as below;

paramone=somerandomvalue; paramtwo=somerandomvalue; paramthree=somerandomvalue;

I want to append a * (asterisk) at the end of every 'somerandomvalue' but before ; (semicolon) like below -

paramone=somerandomvalue*; paramtwo=somerandomvalue*; paramthree=somevrandomalue*;

NOTE - It should only append the strings that have = and ;. There are other strings having ; at the end but doesn't have a =.

I'd leverage sed to do this:

sed 's/;/*;/g' test.txt

Something like that. Assuming every value has that ; at the end of it.

Assuming your input had the following content

$ cat input_file
paramone=somerandomvalue; paramtwo=somerandomvalue; paramthree~somerandomvalue;paramone=somerandomvalue; paramtwo=somerandomvalue; paramthree##somerandomvalue;

Using sed

$ sed s'/\(=[^;]*\)/\1*/g' input_file
paramone=somerandomvalue*; paramtwo=somerandomvalue*; paramthree~somerandomvalue;paramone=somerandomvalue*; paramtwo=somerandomvalue*; paramthree##somerandomvalue;

这也应该有效 - sed s'/=\(.*\)?;/\1*/g' <input_file_name>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM