ske*_*ept 5 python bash scripting perl sed
我有几个文件,比如,a,b,c,我想要的东西
> cat a b c
Run Code Online (Sandbox Code Playgroud)
但是在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()
Run Code Online (Sandbox Code Playgroud)
但我更喜欢在命令行中执行此操作,将一些简单的命令与管道组合在一起 运营商.
有没有一种简单的方法来实现这一目标?
谢谢.
grep(1)并且sed(1)可以为你做这件事grep -H '' <files> | sed 's/:/,/':
$ cat a ; cat b ; cat c
hello
world
from space
$ grep -H '' * | sed 's/:/,/'
a,hello
b,
b,
b,world
c,from space
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
958 次 |
| 最近记录: |