【发布时间】:2016-08-05 18:33:14
【问题描述】:
我正在尝试使用 awk 将一行分成多行。每两个字之后。
输入:
hey there this is a test
输出:
hey there
this is
a test
我可以使用 xargs 来实现它,如下所示:
echo hey there this is a test |xargs -n2
hey there
this is
a test
但是我很想知道如何使用 awk 来实现这一点。这是我正在使用的命令,当然没有给出预期的结果。
echo hey there this is a test | awk '{ for(i=1;i<=NF;i++) if(i%2=="0") ORS="\n" ;else ORS=" "}1'
hey there this is a test
和
echo hey there this is a test | awk '{$1=$1; for(i=1;i<=NF;i++) if(i%2==0) ORS="\n" ;else ORS=" "}{ print $0}'
hey there this is a test
需要了解上述 awk 命令在概念上的错误以及如何修改它以提供正确的输出。假设输入是单行的。
感谢和问候。
【问题讨论】: