Formatting field into the separate column with FS NF field separator bash script

Input file rhel5 contains the following information and there column is separate by the comma, using awk command we separate these single field into three column
linux,23.4,y
ubuntu,52.6,n
kubuntu,24.5,y
fedora,18.9,n
[bash@localhost ~]# awk '{printf "%-15s%-8s%s\n",$1,$2,$3}' FS=\, rhel5
linux----------23.4----------- y
ubuntu--------52.6---------- n
kubuntu-------24.5---------- y
fedora 18.9 n
Fs is single variable character it will will be maintain the space between the two column.if you leave it then it will not separate the field.

[bash@localhost ~]# awk '{printf "%-15s%-8s%s\n",$1,$2,$3}' rhel5
linux,23.4,y
ubuntu,52.6,n
kubuntu,24.5,y
fedora,18.9,n
we perform the same operation in to the different format. In these case NF refers to the number of the field in current record.
[bash@localhost ~]# awk '{ for(i=1;i<=NF;i++) printf("%-15s%c",$i,(i==NF)? ORS : " ")}' FS=, rhel5
linux 23.4 y
ubuntu 52.6 n
[bash@localhost ~]# awk '{ for(i=1;i<=NF;i++) printf("%-15s%c",$i,(i==NF)? ORS : " ")}' rhel5
Don’t Miss Fs if miss it not maintain space between the Word
linux,23.4,y
ubuntu,52.6,n

Post a Comment

0 Comments