1

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"

1
  • 1
    trying to search the file using the key you should add two sample lines, one with hello and other without... all answers so far have missed that point... and on SO you are expected to add the code you tried to solve Commented Oct 27, 2017 at 10:00

3 Answers 3

3

try the below sed expression:

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

It should give you:

hello="80,30,255,64,123"
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Following awk code may help you in same.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.