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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To append new data at the end of each line based on substring of last column
# 1  
Old 01-13-2016
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:
Code:
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:
Code:
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
# 2  
Old 01-13-2016
Hello null7,

Could you please try following and let me know if this helps you.
Code:
awk -F"|" '{n=split($NF, A," ");$(NF+1)=A[n-1]} 1' OFS="|"  Input_file

Output will be as follows.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-13-2016
With your sample data, try:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-19-2016
Quote:
Originally Posted by RavinderSingh13
Hello null7,

Could you please try following and let me know if this helps you.
Code:
awk -F"|" '{n=split($NF, A," ");$(NF+1)=A[n-1]} 1' OFS="|"  Input_file

Output will be as follows.
Code:
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
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 Smilie

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

Quote:
Originally Posted by Don Cragun
With your sample data, try:
Code:
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.
Hi Don Cragun,
This works perfectly. Smilie

Many Thanks.
# 5  
Old 01-19-2016
A Perl alternative:

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

# 6  
Old 01-19-2016
Quote:
Originally Posted by null7
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 Smilie
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.
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append data with substring of nth column fields using awk

Hi guys, I have problem to append new data at the end of each line of the files where it takes whole value of the nth column. My expected result i just want to take a specific value only. This new data is based on substring of 11th, 12th 13th column that has comma seperated value. My code: awk... (4 Replies)
Discussion started by: null7
4 Replies

2. UNIX for Dummies Questions & Answers

Append no of times a column is repeated at the end

Hi folks, Iam working on a bash script, i need to print how many times column 2 repeated at the end of each line. Input.txt COL1 COL2 COL3 COL4 1 XX 45 N 2 YY 34 y 3 ZZ 44 N 4 XX 89 Y 5 XX 45 N 6 YY 84 D 7 ZZ 22 S Output.txt COL1 COL2 COL3 COL4 COL5 1 XX 45 N 3 2 YY 34... (6 Replies)
Discussion started by: tech_frk
6 Replies

3. Shell Programming and Scripting

Help with figuring division and addition based on column data and line numbers

I have a data file in the format of 1234 xxx 1234 xxx 1234 xxx 1234 xxxI want to be able to calculate the following - COLUMN1+((LINENUMBER-1)/365) The output needs to preserve the 2nd column - 1234 xxx 1234.00274 xxx 1234.00548 xxx What is the best way to do this? I am somewhat... (9 Replies)
Discussion started by: ncwxpanther
9 Replies

4. Shell Programming and Scripting

append | to the end of each data in a file

I have a file which has data in the below format: 7810902|6783014102| || |0| |0| |0| |0|||||T|04/13/2006||9423|7421||100|2006-04-13 16:50:28|||2006-04-13 16:50:28|n|51|-1||214 1089929|||||NewSpCreateAction request successful. Activity ID = <826528>||||100|n|2006-04-13 16:50:27|2006-04-13... (3 Replies)
Discussion started by: ankianand88
3 Replies

5. Shell Programming and Scripting

Append text from one file to another based on a search from the end of a document

Hi all, I have output files that are all text files with various different extensions. So, if I submit the input file "job_name.inp", when it finishes I get an output file "job_name.dat". A typical input file looks something like this: $CONTRL SCFTYP=RHF RUNTYP=ENERGY MAXIT=199 MULT=1... (4 Replies)
Discussion started by: marcozd
4 Replies

6. Shell Programming and Scripting

Match data based on two fields, and append to a line

I need to write a program to do something like a 'vlookup' in excel. I want to match data from file2 based on two fields (where both match) in file1, and for matching lines, add the data from two of the fields from file2 to file1. If anyone knows something in perl or awk that can do this, I'd be... (20 Replies)
Discussion started by: jamessmith01
20 Replies

7. Shell Programming and Scripting

Remove duplicate line detail based on column one data

My input file: AVI.out <detail>named as the RRM .</detail> AVI.out <detail>Contains 1 RRM .</detail> AR0.out <detail>named as the tellurite-resistance.</detail> AWG.out <detail>Contains 2 HTH .</detail> ADV.out <detail>named as the DENR family.</detail> ADV.out ... (10 Replies)
Discussion started by: patrick87
10 Replies

8. UNIX for Dummies Questions & Answers

Using sed to extract a substring at end of line

This is the line that I am using: sed 's/^*\({3}*$\)/\1 /' <test.txt >results.txt and suppose that test.txt contains the following lines: http://www.example.com/200904/AUS.txt http://www.example.com/200903/_RUS.txt http://www.example.com/200902/.FRA.txt What I expected to see in results.txt... (6 Replies)
Discussion started by: figaro
6 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question