linux - how to print 3rd field in 3rd column itself -


in file have 3 fields, want print third field in third column output getting first row. please check file , output:

cat filename

1st field     2nd field    3rd field ---------     ---------    ----------- a,b,c,d       d,e,f,g,h    1,2,3,4,5,5  q,w,e,r       t,y,g,t,i    9,8,7,6,5,5 

i'm using following command print third field in third column

cat filename |awk '{print $3}' |tr ',' '\n'  

output printing 3rd field strings in 1st field place, want print in 3rd field area only

first field :- --------------- 1 2 3 4 5  5 

expected output

1st field     2nd field    3rd field ---------     ---------    ----------- a,b,c,d       d,e,f,g,h     1                             2                             3                             4                             5                              5  q,w,e,r       t,y,g,t,i     9                             8                             7                             6                              5                             5 

input

 [akshay@localhost tmp]$ cat file  1st field     2nd field    3rd field  ---------     ---------    -----------  a,b,c,d       d,e,f,g,h    1,2,3,4,5,5   q,w,e,r       t,y,g,t,i    9,8,7,6,5,5 

script

 [akshay@localhost tmp]$ cat test.awk     nr<3 || !nf{ print; next}     {          split($0,d,/[^[:space:]]*/)         c1=sprintf("%*s",length($1),"")         c2=sprintf("%*s",length($2),"")         split($3,a,/,/)         for(i=1; in a; i++)         {                if(i==2)             {                 $1 = c1                 $2 = c2             }             printf("%s%s%s%s%d\n",$1,d[2],$2,d[3],a[i])          }      } 

output

 [akshay@localhost tmp]$ awk -f test.awk file  1st field     2nd field    3rd field  ---------     ---------    -----------  a,b,c,d       d,e,f,g,h    1                             2                             3                             4                             5                             5   q,w,e,r       t,y,g,t,i    9                             8                             7                             6                             5                             5 

explanation

  • nr<3 || !nf{ print; next}

nr gives total number of records being processed or line number, in short nr variable has line number.

nf gives total number of fields in record.

the next statement forces awk stop processing current record , go on next record.

if line number less 3 or not nf (meaning no fields in record blank line), print current record , go next record.

  • split($0,d,/[^[:space:]]*/)

since interested preserve formatting, saving separators between fields on array d here, if have gnu awk can make use of 4th arg split() - lets split line 2 arrays, 1 of fields , other of separators between fields , can operate on fields array , print using separators array between each field array element rebuild original $0.

  • c1=sprintf("%*s",length($1),"") , c2=sprintf("%*s",length($2),"")

here sprintf function used fill space char of field ($1 or $2) length.

  • split($3,a,/,/)

split(string, array [, fieldsep [, seps ] ])

divide string pieces separated fieldsep , store pieces in array , separator strings in seps array. first piece stored in array[1], second piece in array[2], , forth. string value of third argument, fieldsep, regexp describing split string (much fs can regexp describing split input records). if fieldsep omitted, value of fs used. split() returns number of elements created.

loop till long i in a true, came know i=1 , i++ control order of traversal of array, ed morton

  • if(i==2) { $1 = c1 $2 = c2 }

when i = 1 print a,b,c,d , d,e,f,g,h, in next iteration modify $1 , $2 value c1 , c2 created above since interested show once requested.

  • printf("%s%s%s%s%d\n",$1,d[2],$2,d[3],a[i])

finally print field1 ($1), separator between field1 , field2 saved above, d[2], field2 ($2), separator between field2 , field3 , array a element 1 created (split($3,a,/,/)).


Comments