[英]How to move Header and Trailer from files to another file?
I have around 100 text files with close to thousand records in a folder.我有大约 100 个文本文件,文件夹中有近千条记录。 I want to copy header and trailer of these files into a new file with the file name of respective file.
我想将这些文件的头和尾复制到一个新文件中,文件名分别为相应文件的文件名。
So the output i want is as所以我想要的输出是
File_Name,Header,Trailer
is this possible using Unix or Python?这可以使用 Unix 或 Python 吗?
一种方法是使用包含文件的文件夹中的 bash shell:
for file in *; do echo "$file,$(head -1 $file),$(tail -1 $file)"; done
带有别名的 PowerShell 核心单行
gci *.txt |%{"{0},{1},{2}" -f $_.FullName,(gc $_ -Head 1),(gc $_ -Tail 1)}|set-content .\newfile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.