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:

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:

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

What have you tried?

Hello umarsatti,

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

awk -vs1="," -F"|" '{A=$(NF-1);split($NF, B,",");for(i in B){C=C?C s1 A B:A B;$NF=C;}C=""} 1' OFS="|"  Input_file

Output will be as follows.

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

1 Like

Thanks Ravinder, it works like a charm.

Can you please explain your code as well?

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:

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 < n ? "," : "")
	$ToField = o
}
1' file

If you are using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

Hello umarsatti,

Following is the explanation for same.

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:A B;         ######## 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

1 Like
$ 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
1 Like

A bit of Perl

perl -plaF'\|' -e '$F[-1] =~ s/(\d+)/$F[3]$1/g; s/[\d,]+$/$F[-1]/' file.txt
USS|AWCC|AFGAW|93|9370
USSAA|Roshan TDCA|AFGTD|93|9372,9379
ALB|Vodafone|ALBVF|355|35569
ALGEE|Wataniya (Nedjma)|DZAWT|213|21350,213550

---------- Post updated at 12:53 PM ---------- Previous update was at 12:10 PM ----------

Afterthought

perl -ple '@n = /(\d+)/g; for $i (@n[1..$#n]){s/$i/$n[0]$i/}' umarsatti.file
1 Like