Input file:
$ cat details.txt
AX|23.45|1932323|A|VI|-|Y|0
TY|93.45|2932323|B|VI|-|Y|1
RE|63.25|8932323|A|VI|0|N|1
AY|83.85|0932323|C|VI|-|Y|0
Required:
Print all columns from the above file except column number 2 and 7.
i.e. required output:
AX|1932323|A|VI|-|0
TY|2932323|B|VI|-|1
RE|8932323|A|VI|0|1
AY|0932323|C|VI|-|0
Basically for the above file we have to print column # 1,3,4,5,6,8
i.e.
$ awk '
BEGIN{FS=OFS="|"}{print $1,$3,$4,$5,$6,$8}
' details.txt
But if number of fields is very large on the input file, the above method is not going to be so useful. So here is another technique.
$ awk '
BEGIN{FS=OFS="|"}
{ for (i=1; i<=NF;i++)
if( i==2 || i==7 ) continue
else
printf("%s%s", $i,(i!=NF) ? OFS : ORS)}
' details.txt
And if you want to exclude a range of column numbers (say exclude column 3 to column 6) here is my earlier post
An additional tip:
Suppose you need to generate the print 'statement' for printing a number of consecutive fields for an awk program, here is a quick way:
$ seq -s ",$" 1 8 | sed 's/.*/{print $&}/'
Output:
{print $1,$2,$3,$4,$5,$6,$7,$8}
Or you can use the the for loop mentioned above.
1 comment:
awk '{for (i=1;i<=NF;i+=10) { printf "%s ",$i};printf "\n"}'
Post a Comment