简体   繁体   中英

Extracting 200 lines from all the files in a folder

I have a folder with a lot of CSV files. 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. So I would have several new files with 200 lines within each of them. I need to complete this task in Linux Ubuntu and using OS shell commands. 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
  3. xargs command that take the list of files and run on it the command pass with the sh -c option
  4. The command that display the 200 first line of a file and put it in a filename with "headed" extension

With this line you have a solution that could be extensible for another usecase near the one you've written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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