我需要在文件末尾显示文件的行数作为预告片。
我有0000000000
(10 0' s),所以如果行数为2
,则字符串应为0000000002
,如果文件中的行数为{{1}那么字符串应该是120
。我需要在文件的末尾显示这个字符串作为预告片。请帮助在unix中完成此操作。
答案 0 :(得分:2)
您可以尝试:
printf "%010d" $(wc -l < file.txt) >> file.txt
答案 1 :(得分:1)
简单的sed
在线人员可以完成任务。
sed "$ s/$/\n0000000000$(wc -l < input)/" input
<强>测试强>
$ cat test
hello
world
how
are
you
$ sed "$ s/$/\n0000000000$(wc -l < test)/" test
hello
world
how
are
you
00000000005
它的作用??
$
匹配文件中的最后一行
s/$/\n0000000000$(wc -l < input)/"
在s
$
行尾,0000000000$(wc -l < input)
注意
您可以在sed中使用-i
选项来编辑文件,以便在原始文件中进行更改
$ sed -i "$ s/$/\n0000000000$(wc -l < test)/" test
$ cat test
hello
world
how
are
you
00000000005
答案 2 :(得分:1)
你可以试试awk
awk '{print}END{printf("%010i\n", NR)}' file