Saturday, January 12, 2008

Blank column in file - AWK newbie


Objective:

1)Find the line and column number of the "blank" columns of the following file (testinfo.id)
2)Replace them with "NA"

$ cat testinfo.id
3.45:343:123:
0.9:1.2:456:1.23
5.45:353::12
:363:125:18
0.2:22:12.34:12.9

Solution:
1)
$ awk 'BEGIN {print "Blank Columns found in\nLine\tColumn"}
{for(k=0;k<=NF;k++)
if ( $k == "" )
print NR "\t" k
}' FS=":" testinfo.id


Blank Columns found in
Line Column
1 4
3 3
4 1

2)
$ awk 'BEGIN{OFS=FS=":"}{ for(k=0;k<=NF;k++)
if ( $k == "" )
$k="NA"
print $0}' testinfo.id


3.45:343:123:NA
0.9:1.2:456:1.23
5.45:353:NA:12
NA:363:125:18
0.2:22:12.34:12.9

No comments:

© Jadu Saikia www.UNIXCL.com