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

4 comments:

yash said...

very nice post

yash said...

very help full post

Karan Bohra said...

sed -e '
s/,/\n/2;s/,/\n/2
s/\n\n/,NA,/
s/\n\(.\).*\n/,\1,/
' input_file

Karan Bohra said...

< file.txt perl -F\, -pale '$_ = join $", map { "[$_]" } @F' |
dc -e "
[q]sq
[SM]sa
[z 3 <a z 3 <b]sb
[s0[NA]q]sc
[dZ 0 =c a]sd
[SMSMSM]se
[LMn [,]n lF1-dsF 1 <p]sp
[? z 0 =q zsF lbx ldx lex lpx LMp c l?x]s?
l?x
"

© Jadu Saikia www.UNIXCL.com