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.
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 .
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.