开发者

Transposing two fields in awk

开发者 https://www.devze.com 2023-04-12 18:15 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号