将数字添加到文件中每一行的开头
如何将数字添加到文件中每一行的开头?
例如:
This is the text from the file.
变成:
000000001 This is 000000002 the text 000000003 from the file.
How can I add numbers to the beginning of every line in a file?
E.g.:
This is the text from the file.
Becomes:
000000001 This is 000000002 the text 000000003 from the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要使用 cat 或任何其他不是为此目的设计的工具。使用该程序:
示例:
因为 nl 是为此而设计的;-)
Don't use cat or any other tool which is not designed to do that. Use the program:
Example:
Because nl is made for it ;-)
AWK 的 printf、
NR
和$0
可以轻松精确且灵活地控制格式:AWK's printf,
NR
and$0
make it easy to have precise and flexible control over the formatting:您正在寻找
nl(1)
命令:-w9
要求输入九位数长的数字;-nrz
要求将数字格式化为右对齐并用零填充。You're looking for the
nl(1)
command:-w9
asks for numbers nine digits long;-nrz
asks for the numbers to be formatted right-justified with zero padding.cat -n thefile
可以完成这项工作,尽管数字的格式略有不同。cat -n thefile
will do the job, albeit with the numbers in a slightly different format.最简单、最简单的选择是
请参阅上面的评论,了解为什么 nl 并不是真正的最佳选择。
Easiest, simplest option is
See comment above on why nl isn't really the best option.
这是一个 bash 脚本,也可以执行此操作:
Here's a bash script that will do this also: