awk 每行打印 x3 次?

asa*_*sad 0 awk

有了这个 awk 单行 awk '{lines[NR]=$0}{for(i=0;i<3;i++) print $lines[i]} awk.write

awk.write

this line 1 no un1x
this lines 22 0
butbutbut this 33 22 has unix
but not 1
THIS is not
butbutbut ffff
second line
Run Code Online (Sandbox Code Playgroud)

打印为

this line 1 no un1x
this line 1 no un1x
this line 1 no un1x
this lines 22 0
this lines 22 0
this lines 22 0
butbutbut this 33 22 has unix
butbutbut this 33 22 has unix
butbutbut this 33 22 has unix
but not 1
but not 1
but not 1
THIS is not
THIS is not
THIS is not
butbutbut ffff
butbutbut ffff
butbutbut ffff
second line
second line
second line
Run Code Online (Sandbox Code Playgroud)

然而,似乎$在这里可以使用不同的工作方式,因为如果不运行它,lines[i]我就会得到

this line 1 no un1x


this line 1 no un1x
this lines 22 0

this line 1 no un1x
this lines 22 0

this line 1 no un1x
this lines 22 0

this line 1 no un1x
this lines 22 0

this line 1 no un1x
this lines 22 0

this line 1 no un1x
this lines 22 0
Run Code Online (Sandbox Code Playgroud)

当与其输出相同的运行时,该点lines[0]返回空结果$lines[i],如上面部分所示printed as。有没有更好的方法来做到这一点,我不喜欢$在这方面使用。谢谢。

hek*_*mgl 5

我会用sed

sed 'p;p' file
Run Code Online (Sandbox Code Playgroud)

您只需要两个p操作,因为sed默认情况下会打印该行一次。


有了awk它可以:

awk '1;1;1' file
Run Code Online (Sandbox Code Playgroud)

与以下内容相同:

awk '1{print}1{print}1{print}' file
Run Code Online (Sandbox Code Playgroud)

或者

awk '{print}{print}{print}'
Run Code Online (Sandbox Code Playgroud)

或者简单地:

awk '{print;print;print}' file
Run Code Online (Sandbox Code Playgroud)