Aastha
Aastha

Reputation: 23

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 =.

Upvotes: 1

Views: 177

Answers (3)

Harshita
Harshita

Reputation: 85

This should also work - sed s'/=\(.*\)?;/\1*/g' <input_file_name>

Upvotes: 0

sseLtaH
sseLtaH

Reputation: 11227

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;

Upvotes: 3

JJ Asghar
JJ Asghar

Reputation: 609

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.

Upvotes: 3

Related Questions