append value to variable using sed - shell script append value to variable using sed - shell script unix unix

append value to variable using sed - shell script


try the below sed expression:

sed 's/"$/,123"/' inputfile

It should give you:

hello="80,30,255,64,123"


using awk

awk 'sub(/"$/,",123&")+1' infile

Test Results:

$ echo 'hello="80,30,255,64"' | awk 'sub(/"$/,",123&")+1'hello="80,30,255,64,123"

sub(/"$/,",123&")+1 is incase if sub(), does not return non zero, then still print such line

I am trying to search the file using the key and add 123 to the end of line.

Can't find any key here, if you mean to say hello is your key then

awk '/^hello=/{ sub(/"$/,",123&") }1' infile > outfile

Explanation:

  • /^hello=/ - look for line starts with hello=

(^ indicates the beginning of the string.)

  • sub(/"$/,",123&") - substitute "$

( $ indicates the end of the string )

with comma, 123 and &

( special character & appears in replacement, it stands for the precise substring that was matched by regexp. )


Following awk code may help you in same.

awk -F"\"" '{$2=$2 ",123"} 1' OFS="\""  Input_file