简体   繁体   中英

append value to variable using sed - shell script

I am trying to search the file using the key and add 123 to the end of line. and other than the key "hello" I dont have values of hello. How can I achieve this sed or awk?

file contents

hello="80,30,255,64"

expected output

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

try the below sed expression:

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

It should give you:

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

以下awk代码可能会帮助您。

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

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

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