Transposing two fields in awk
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 exa开发者_Python百科mple:
c|b|a|d
c|b|a|d
...
c|b|a|d
Thanks in advance!
Here is another solution: swap the first and third field, then print:
awk -F '|' '{ temp=$1; $1=$3; $3=temp; print }' data.txt
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 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
精彩评论