如何在每行之前打印文件内容和文件名?
我有几个文件,比如说,a,b,c,我想要类似的东西
> cat a b c
,但在 a 的开头行中带有“a”。 “b”位于 b 行的开头,“c”位于 c 行的开头。 我可以使用 python 来执行此操作:
#!/bin/env python
files = 'a b c'
all_lines = []
for f in files.split():
lines = open(f, 'r').readlines()
for line in lines:
all_lines.append(f + ',' + line.strip())
fout = open('out.csv', 'w')
fout.write('\n'.join(all_lines))
fout.close()
但我更喜欢在命令行中执行此操作,将一些简单的命令与管道 | 结合起来。操作员。
有没有一种简单的方法可以实现这一点?
谢谢。
I have several files, say, a,b,c, I would like to something like
> cat a b c
but with "a," in the beginning lines of a. "b," in the beginning of the lines of b and "c," in the beginning of the lines of c.
I can do this using python:
#!/bin/env python
files = 'a b c'
all_lines = []
for f in files.split():
lines = open(f, 'r').readlines()
for line in lines:
all_lines.append(f + ',' + line.strip())
fout = open('out.csv', 'w')
fout.write('\n'.join(all_lines))
fout.close()
but I would prefer to do it in the command line, combining some simple commands with the pipe | operator.
Is there a easy way of accomplishing this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会做的。
will do it.
grep(1)
和sed(1)
可以为您执行此操作:grep -H ''; | sed 's/:/,/'
:grep(1)
andsed(1)
can do this for you:grep -H '' <files> | sed 's/:/,/'
:您还可以使用
awk(1)
程序:)You can also use the
awk(1)
program :)