Tuesday, September 8, 2009

Print first character of a field - awk substr


Input file:

$ cat file.txt
8965,1212,c32,1
1221,9000,d90,0
1222,7823,,2
9012,1901,c12,7
9012,1342,t90,9

Output Required: If 3rd field is non empty, print only the first character of the value, else(i.e. when the field is blank) print "NA" in the 3rd field.
i.e. required output:

8965,1212,c,1
1221,9000,d,0
1222,7823,NA,2
9012,1901,c,7
9012,1342,t,9


The awk program:

$ awk '
BEGIN {FS=OFS=","}
{ if ( length($3) ) { $3 = substr($3, 0, 1) }
else { $3 = "NA" }
print
}
' file.txt

Related post:
- Brief about awk substr function
- Blank column in file - awk newbie
- Count non empty field in file using awk

2 comments:

yash said...

very nice post

yash said...

very help full post

© Jadu Saikia www.UNIXCL.com