Taking nth column and putting its value in n+1 column using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking nth column and putting its value in n+1 column using awk
# 1  
Old 12-31-2015
Taking nth column and putting its value in n+1 column using awk

Hello Members,

Need your expert opinion how to tackle below.

I have an input file that looks like below:

Code:
USS|AWCC|AFGAW|93|70
USSAA|Roshan TDCA|AFGTD|93|72,79
ALB|Vodafone|ALBVF|355|69
ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550

I like output file in below format:

Code:
USS|AWCC|AFGAW|93|9370
USSAA|Roshan TDCA|AFGTD|93|9372,9379
ALB|Vodafone|ALBVF|355|35569
ALGEE|Wataniya (Nedjma)|DZAWT|213|21350,213550

What I am trying to do is, I am taking 4th column and trying to append it to 5th (last column), however I am facing difficulty in putting value of 4th column when 5th column has ",". As I want to put 4th column value to 5th column comma separated values as well.

Regards,
Umar
# 2  
Old 12-31-2015
What have you tried?
# 3  
Old 12-31-2015
Hello umarsatti,

Could you please try following and let me know if this helps.
Code:
awk -vs1="," -F"|" '{A=$(NF-1);split($NF, B,",");for(i in B){C=C?C s1 A B[i]:A B[i];$NF=C;}C=""} 1' OFS="|"  Input_file

Output will be as follows.
Code:
USS|AWCC|AFGAW|93|9370
USSAA|Roshan TDCA|AFGTD|93|9372,9379
ALB|Vodafone|ALBVF|355|35569
ALGEE|Wataniya (Nedjma)|DZAWT|213|21350,213550

EDIT: I am sorry Anbu, I guess we were replying on the same time, so didn't see your reply.

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-31-2015 at 02:43 AM.. Reason: Added apologies to Anbu as he has asked user a question.
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 12-31-2015
Thanks Ravinder, it works like a charm.

Can you please explain your code as well?
# 5  
Old 12-31-2015
Since for index in array_name produces the index list in an unspecified order, the order of the output subfields will not necessarily be in the same order as the subfields in the input when using RavinderSingh13's suggested script. If that matters for your application, you could try something more like:
Code:
awk -v FromField=4 -v ToField=5 '
BEGIN {	FS = OFS = "|"
}
{	n = split($ToField, f, /,/)
	o = ""
	for(i = 1; i <= n; i++)
		o = o $FromField f[i] (i < n ? "," : "")
	$ToField = o
}
1' file

If you are using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-31-2015
Hello umarsatti,

Following is the explanation for same.
Code:
awk -vs1="," -F"|" '{           ######## taking a variable named s1 whose value is comma(,) and setting field separator to |
A=$(NF-1);                      ######## Now making a variable named A whose value is the second last field's value.
split($NF, B,",");              ######## Now using split function I am splitting value of last column $NF and storing it to an array named B with delimiter comma(,).
for(i in B){                    ######## Now going into elements of array B
C=C?C s1 A B[i]:A B[i];         ######## now creating a variable named C whose value is array A's value with array B's value, it is keep appending if it has more than one values to it into C variable by conditional operators ? and :
$NF=C;}                         ######## Now making $NF(last field's) value to variable C's value.
C=""}                           ######## Now nullifying the value of variable C.
1'                              ######## awk works on method of condition and then pattern, here I am mentioning 1 which means condition is TRUE and no action so default action will occur which is printing the line.
OFS="|" Input_file              ######## mentioning the output field seprator to | and mentioning Input_file name.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 12-31-2015
Code:
$ awk -F"|" -v OFS="|" ' $NF { $NF=","$NF; gsub(",",","$(NF-1),$NF); sub(",","",$NF) } 1 ' file
USS|AWCC|AFGAW|93|9370
USSAA|Roshan TDCA|AFGTD|93|9372,9379
ALB|Vodafone|ALBVF|355|35569
ALGEE|Wataniya (Nedjma)|DZAWT|213|21350,213550

This User Gave Thanks to anbu23 For This Post:
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. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

6. Shell Programming and Scripting

Putting mysql column value in variable

Hello, i have the following code : somevariable=`mysql -u root --password="" databasename -e "select count(column_name) from table_name where id=1 and id_age=2 ; " --skip-column-names -s -N` if ] then .... It gives the message ")syntax error: invalid arithmetic operator (error... (2 Replies)
Discussion started by: juve11
2 Replies

7. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

8. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

10. Shell Programming and Scripting

Using tr, sed or awk to delete text from nth column only

Hi everyone, this is my first post here, I hope someone can help me. I have a file which I need to delete characters '_F3' from the end of the text in the first column. The problem is that the characters may also occur elsewhere in the file (i.e. second columns onwards). I tried sed (thinking I... (6 Replies)
Discussion started by: hlwright
6 Replies
Login or Register to Ask a Question