【发布时间】:2020-07-21 23:33:50
【问题描述】:
这个 python 3 代码完全符合我的要求
from pathlib import Path
def minify(src_dir:Path, dest_dir:Path, n: int):
"""Write first n lines of each file f in src_dir to dest_dir/f"""
dest_dir.mkdir(exist_ok=True)
for path in src_dir.iterdir():
new = [x.rstrip() for x in list(path.open().readlines())][:n]
dest_path = dest_dir.joinpath(path.name)
dest_path.open('w').write('\n'.join(new))
有没有办法在 bash 中做同样的事情,也许是 xargs?
ls src_dist/* | xargs head -10
显示我需要的内容,但我不知道如何将该输出路由到正确的文件。
【问题讨论】:
-
绝对有可能,
for f in src_dist/* ; do sed -n '1,10p' "$f" > otherDir/"$f".hdr ; done之类的东西会让你开始。如果您卡在bash解决方案上,请发布新的 Q。确保在弄清楚这一点时设置一个测试目录结构。很容易弄乱你的核心文件。祝你好运。