4

Suppose that a file looks like:

a|b|c|d
a|b|c|d
...
a|b|c|d

How do I transpose two fields, for example:

c|b|a|d
c|b|a|d
...
c|b|a|d

Thanks in advance!

Russell
  • 2,692
  • 5
  • 23
  • 24

3 Answers3

4

Here is another solution: swap the first and third field, then print:

awk -F '|' '{ temp=$1; $1=$3; $3=temp; print }' data.txt
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
2

At least if memory serves, something on this general order should work:

BEGIN { FS="|"; }

    { printf("%s|%s|%s|%s\n", $3, $2, $1, $4); }
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

Jerry was 'right', but there is a much more concise way to accomplish this

awk -F\| '{print $3FS$2FS$1FS$4}' input.csv

FS stands for Field Separator You Could change it to $3" "$2" ", etc.. if its easier

matchew
  • 19,195
  • 5
  • 44
  • 48