0

I am trying to create a file (using AWK, but do not mind switching if another command is easier) that has a unique string in each line (183745 lines total). I am trying to make a file as such:

 line1
 line2
 line3
 ....
 line183745

With poor knowledge of AWK, and failure to find a similar example, I have unsuccessfully tried (with 10 lines for this example):

awk '{ i = 1; while (i < 10) { print "line$i \n"}; i++ }'

And this leads to no error or output. Thank you.

4

4 回答 4

3

为什么要复杂化?

seq -f "line%06g" 3
line000001
line000002
line000003

seq -f "line%06g" 183745 >newfile
于 2013-11-08T21:03:35.713 回答
2

您需要将其放在 BEGIN 块中,因为您不处理任何输入行。

awk 'BEGIN { i = 1 ; while (i <= 10) { print "line"i ; i++ } }'
于 2013-11-08T20:53:33.850 回答
1

如果我这样做,我会用seq或在vim.

但由于其他人已经发布了seq经典awk解决方案,我会添加另一个awk解决方案来娱乐。

一个非常“有用”的命令yes可以帮助我们:

awk '$0="line"NR;NR==183745{exit}'

用 1-10 进行测试,例如:

kent$ yes|awk '$0="line"NR;NR==10{exit}'
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
于 2013-11-08T21:47:02.457 回答
1

awk默认情况下就像一个过滤器。在您的情况下,它只是阻止输入。例如,通过明确没有输入来取消阻止它。

awk '...' </dev/null
于 2013-11-08T20:52:28.440 回答