jksl
jksl

Reputation: 323

Print columns in a reoccurring pattern with awk

I have quite wide files with tab separated columns:

Donna   25.07.83   Type1   A   B   C   D  E   F   G   H  ....
Adam    17.05.78   Type2   A   B   C   D  E   F   G   H  ....

I'd like to print out everything, but after the third column print a tab after every two columns..

Donna   25.07.83   Type1   AB   CD  EF   GH  ....
Adam    17.05.78   Type2   AB   CD  EF   GH  ....

I think there probably is a more clever way to do this than

awk '{OFS="\t"} {print $1, $2, $3, $4$5, $6$7, $8$9}' 

and so on, particularly because my files have over 1000 columns in them. Can awk do this?

Upvotes: 1

Views: 187

Answers (2)

Faiz
Faiz

Reputation: 16245

Quite yucky, but works:

awk '{printf "%s\t%s\t%s",$1,$2,$3; for(i=4;i<=NF;i+=2) printf "\t%s%s",$i,$(i+1); print ""}' wide.txt

NF is an awk variable whose value is a number that tells you how many columns the current line has. You'll find it in the manual.

Let's take it apart:

#!/usr/bin/awk -f

{ 
  printf "%s\t%s\t\%", $1, $2, $3;  # print the first 3 columns, explicitly 
                                    # separated by TAB. No NEWLINE will be printed.

  # We want to print the remaining columns in pairs of $4$5, $6$7

  for( i = 4; i <= NF ; i+=2 )       # i is 4, then 6, then 8 ... till NF (the num. of the final column)
     printf "\t%s%s", $i, $(i+1);   # print \t$4$5, then \t$6$7, then \t$8$9 

  print ""                          # We haven't print the end-of-line NEWLINE
                                    # yet, so this empty print should do it.
}

Upvotes: 1

Vijay
Vijay

Reputation: 67211

awk '{for(i=1;i<=NF;i++){if(i>=4){$i=$i$(i+1);$(i+1)="";i+=1}}print}' your_file

tested:

> cat temp
Donna   25.07.83   Type1   A   B   C   D  E   F   G   H
Adam    17.05.78   Type2   A   B   C   D  E   F   G   H
> awk '{for(i=1;i<=NF;i++){if(i>=4){$i=$i$(i+1);$(i+1)="";i+=1}}print}' temp
Donna 25.07.83 Type1 AB  CD  EF  GH 
Adam 17.05.78 Type2 AB  CD  EF  GH 

Upvotes: 1

Related Questions