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"
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. )
trying to search the file using the keyyou should add two sample lines, one withhelloand other without... all answers so far have missed that point... and on SO you are expected to add the code you tried to solve