To append new data at the end of each line based on substring of last column

Hi guys,
I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column.

Input file xxx.csv:

U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0
U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0
A1234|1-8X|apple|1-8X|Cls|1-8X|0.1 /sac/apple 1234 0
A5678|1-9X|lime|1-9X|Cls|1-9X|0.1 /sac/lime 56789 0

Expected Output File xxx.csv:

U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0|12345
U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0|5678
A1234|1-8X|apple|1-8X|Cls|1-8X|0.1 /sac/apple 1234 0|1234
A5678|1-9X|lime|1-9X|Cls|1-9X|0.1 /sac/lime 56789 0|56789

Any idea?

Thanks

Hello null7,

Could you please try following and let me know if this helps you.

awk -F"|" '{n=split($NF, A," ");$(NF+1)=A[n-1]} 1' OFS="|"  Input_file

Output will be as follows.

U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0|12345
U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0|5678
A1234|1-8X|apple|1-8X|Cls|1-8X|0.1 /sac/apple 1234 0|1234
A5678|1-9X|lime|1-9X|Cls|1-9X|0.1 /sac/lime 56789 0|56789

Thanks,
R. Singh

1 Like

With your sample data, try:

awk '{print $0 "|" $3}' xxx.csv > $$.tmp && cp $$.tmp xxx.csv && rm -f $$.tmp

As always, if you're trying this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

Hi RavinderSingh13, thank i've tried it with the sample input. But the problem is when i change the input (maintain the exact column only data different) it messed up. Can u explain a bit what the code actually did?

Thanks :b:

---------- Post updated at 12:51 PM ---------- Previous update was at 12:46 PM ----------

Hi Don Cragun,
This works perfectly. :b:

Many Thanks.

A Perl alternative:

perl -i -pale '$_="$_|$F[2]"' null7.file

Hello null7,

As you haven't mentioned which kind of data change you have done, so I can't say about it. Following is the explanation for code.

awk -F"|"                  ###### Making Field separator as | (pipe)
'{n=split($NF, A," ");     ###### using split which is awk's in built utility to split the field/variable accordingly into an array where we could choose our own delimiter, like here I have taken field $NF(which means last field of line), array named A, with delimiter space. So split(Variable/Field/Line, Array_name, delimiter). Also taking a variable named n which will have total number of elements that array A has in it. So that we could use this further in code.
$(NF+1)=A[n-1]}            ###### Making a new field next to last field which $NF by making it to $(NF+1) it will add a new field to current last field whose value will be equal to array A's 2nd last element which is actually line's second last field too(Because we have taken space as delimiter.)
1'                         ###### putting 1 here, awk works on basis of condition and action, so if condition is TRUE action will be performed, here I have made condition TRUE by mentioning as 1 but didn't mention any action so default action which is print in awk will happen so it will print the line.
OFS="|"  Input_file        ###### Mentioning the output field separator as | and mentioning Input_file here.
 

Thanks,
R. Singh