Abdel
Abdel

Reputation: 6106

awk: print certain columns and a series of concsecutive columns

If I want to print, say, the fourth column till the tenth column, I can use:

awk '{for( i=4; i<=10; i++ ){printf( "%s ", $i )}; printf( "\n"); }

But what if I want to print columns 1, 3, and then 4-10, and then 12?

Upvotes: 0

Views: 38

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk solution:

awk '{ for(i=1;i<=12;i++) if(i!~/^(2|11)$/) printf "%s ",$i; print "" }' file
  • if(i!~/^(2|11)$/) - ensures that it's not the 2nd or the 11th field. You can extend the alternation group (2|11|...) to multiple field numbers that should be skipped

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133518

Try following and let me know if this helps.

awk '{printf("%s %s",$1,$3);for( i=4; i<=10; i++ ){printf( "%s ", $i )}; printf(" %s\n",$12);}'  Input_file

Upvotes: 1

Related Questions