如何连续编号文件的奇数行?

git*_*git 0 bash shell awk sed

我想要做的只是添加一个包含巨大文件数量的列:

xxx xxxxx xxxx
xxx xxxxx xxxx
xxx xxxxx xxxx
xxx xxxxx xxxx
xxx xxxxx xxxx
Run Code Online (Sandbox Code Playgroud)

要获得下一个输出:

xxx 1 xxxx xxxxx
xxx xxxx xxxx
xxx 2 xxxx xxxxx
xxx xxxx xxxx
xxx 3 xxxx xxxxx
Run Code Online (Sandbox Code Playgroud)

我尝试了一些东西,awk '{print NR % 2==1 etc ...}但它不起作用

有什么建议吗?

提前谢谢了

che*_*ner 7

你走在正确的轨道上

awk 'NR%2 { $1 = $1" "++i}; 1;' file.txt
Run Code Online (Sandbox Code Playgroud)

NR%2奇数行的评估为真.结果赋值将第一个字段替换为第一个字段中的值加上一个从(从0开始)递增然后连接的数字.该1;结果始终为true,并应用默认操作(打印)到该行.更长但更明确的等价物是NR%2 { $1 = $1" "++i}; {print}.