1

Input file:

ID,Name,Values 
1,A,vA|A2
2,B,VB

Expected output:

1,A,vA|VA2|vA3
2,B,VB 

Search file for a given ID and then append a given value in the values {field}

use case : append 'testvalue' to the values filed of ID = 1

Problem is : How tho cache the line found ?

sed's s can be used to substitution, I used sed's p {print but of no use }.

5 Answers 5

2

Just set n to ID of the row you want to update and x to the value:

# vA3 to entry with ID==1
$ awk -F, '$1==n{$0=$0"|"x}1' n=1 x="vA3" file
ID,Name,Values
1,A,vA|A2|vA3
2,B,VB

# TEST_VALUE to entry with ID==2
$ awk -F, '$1==x{$0=$0"|"v}1' x=2 v="TEST_VALUE" file
ID,Name,Values
1,A,vA|A2
2,B,VB|TEST_VALUE

Explanation:

  • -F, sets the field separator to be a comma.
  • $1==x checks if the line we are looking at contains the ID we want to change. Where $1 is the first field on each line and x is the variable we define.
  • If the previous condition was true then follow block gets executed {$0=$0"|"v} where $0 is the variable containing the whole line so we are just appending the string "|" and value of the variable v to end of the line.
  • The trailing 1 is just a shortcut in awk to say print the line. The 1 is the condition for the block which is evaluated to true and since no block is provide awk executes the default block {print $0}. Explicitly the script would be awk -F, '$1==n{$0=$0"|"x}{print $0}' n=1 x="vA3" file.
Sign up to request clarification or add additional context in comments.

2 Comments

A brief explanation will be helpful
Funny I was thinking the same thing about your question. See the edit, I have added an explanation.
2

The following script is doing something similar to Your need. It is in pure bash.

#!/usr/bin/bash
[ $# -ne 2 ] && echo "Arg missing" && exit 1;
while read l; do
   [ ${l%%,*} == "$1" ] && l="$l|$2"
   echo $l
done <infile

You can use as script <ID> <VALUE>. Example:

$ ./script 1 va3
ID,Name,Values
1,A,vA|A2|va3
2,B,VB
$ cat infile
ID,Name,Values 
1,A,vA|A2
2,B,VB

Comments

1

or may be this?

awk '/vA/ { $NF=$NF"|VA2" } 1' FS=, OFS=,


$ echo "1,A,vA
2,B,VB" | awk '/vA/ { $NF=$NF"|VA2" } 1'  FS=, OFS=,
1,A,vA|VA2
2,B,VB

Edit 1: awk started supporting in-file substitution recently. But with your requirement it is best to go with sed solution that Kent has posted above.

$ cat file
ID,Name,Values
1,A,vA|A2
2,B,VB

$ awk '$1==1 { $NF=$NF"|vA3" } 1' FS=, OFS=, file
ID,Name,Values
1,A,vA|A2|vA3
2,B,VB

Comments

1

are your looking for this?

kent$ echo "1,A,vA
2,B,VB"|sed '/vA/s/$/|VA2/'
1,A,vA|VA2
2,B,VB

EDIT check the ID, then replace

kent$  echo "ID,Name,Values 
1,A,vA|A2
2,B,VB"|sed 's/^1,.*/&|vA3/'
ID,Name,Values 
1,A,vA|A2|vA3
2,B,VB

& means the matched part. that would be what you meant "cache"

1 Comment

@saurabhshashank check the EDIT in answer
0

sed ' 1 a\ |VA2|vA3 ' file1.txt

1 Comment

Try to Explain your Answer.

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.