简体   繁体   English

从文件夹中的所有文件中提取 200 行

[英]Extracting 200 lines from all the files in a folder

I have a folder with a lot of CSV files.我有一个包含很多 CSV 文件的文件夹。 There are subfolders also.还有子文件夹。 For each such file I need to extract the first 200 lines (to make a db sub sample) creating another file with these lines.对于每个这样的文件,我需要提取前 200 行(以制作 db 子样本),用这些行创建另一个文件。 So I would have several new files with 200 lines within each of them.所以我会有几个新文件,每个文件都有 200 行。 I need to complete this task in Linux Ubuntu and using OS shell commands.我需要在 Linux Ubuntu 并使用 OS shell 命令完成此任务。 Is there a way to do this?有没有办法做到这一点? Thanks in advance.提前致谢。

This line could respond to your need:此行可以满足您的需求:

find . -type f -name "*.csv" -print|xargs -I@ sh -c 'head -n200 @ > @.headed'

This line is made with different part:这条线由不同的部分组成:

  1. Find the files that must be "headed"找到必须“带头”的文件
  2. The output of the find command is just a list of files find命令的output只是文件列表
  3. xargs command that take the list of files and run on it the command pass with the sh -c option xargs 命令获取文件列表并在其上运行命令通过 sh -c 选项
  4. The command that display the 200 first line of a file and put it in a filename with "headed" extension显示文件的第一行 200 并将其放入带有“headed”扩展名的文件名中的命令

With this line you have a solution that could be extensible for another usecase near the one you've written.有了这一行,您就有了一个解决方案,该解决方案可以扩展到您编写的那个附近的另一个用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM