0

I am dealing with a txt file and trying to keep the first column/field in place, but shift the 2nd column down by 1 row. I tried this, but it gave the error unmatched .'s.

awk  '{for(i=1; i<NR; i++) FNR==i {print i, $(i+1)}}’ filename
  • Could you please provide input and output example? – rush May 2 '13 at 22:39
  • What about the first line? Can you give the first three lines as they are and as they should be? – Hauke Laging May 2 '13 at 22:39
3

I'm guessing you want

awk '{tmp = $2; $2 = prev; prev = tmp; print}' filename

Any whitespace between the columns will be collapsed to a single space.

example

$ seq 12 | paste - - -
1       2       3
4       5       6
7       8       9
10      11      12

$ seq 12 | paste - - - | awk '{tmp = $2; $2 = prev; prev = tmp; print}' 
1  3
4 2 6
7 5 9
10 8 12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.