[英]Swap two columns with awk
I try to swap two columns in a text file, the field separator is the pipe '|' 我尝试在文本文件中交换两列,字段分隔符是管道'|' sign.
标志。
I found and tried 我发现并尝试过
awk ' { t = $1; $1 = $2; $2 = t; print; } ' input_file
it works when the field separator is the tab. 当字段分隔符是选项卡时,它可以工作。
I also tried 我也试过了
awk -F\| '{print $2 $1 }' input_file
but I do not manage to specify the '|' 但我没有设法指定'|' as output field separator.
作为输出字段分隔符。
You need to define the OFS, Output Field Separator. 您需要定义OFS,输出字段分隔符。 You can do it with either of these ways:
您可以使用以下任一方式执行此操作:
awk 'BEGIN{FS=OFS="|"} {t=$1; $1=$2; $2=t; print} ' input_file
or 要么
awk -v OFS="|" -v FS="|" ' {t=$1; $1=$2; $2=t; print} ' input_file
or ( thanks Jaypal !) 或者( 感谢Jaypal !)
awk '{t=$1; $1=$2; $2=t; print}' OFS="|" FS="|" input_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.