[Linux] Command to split CSV, text files, etc. by specified number of lines

Tadashi Shigeoka ·  Tue, April 8, 2014

I had a case where I needed to split a CSV file by a certain number of lines, so I used the Linux split command to handle it.

Linux

Prerequisites

  • Want to split users.csv every 100 lines
  • Add the name users- at the beginning of the split file names
  • Create the split files under the tmp directory

1. Split the target file every 100 lines

Use the split command to split the file.

split -l 100 users.csv tmp/users-

2. Add .csv extension to the split files

Use a for loop to add .csv to the end of each filename using the mv command.

cd tmp/
for filename in users-*; do mv $filename $filename.csv; done

That’s it.

Reference Information

That’s all from the Gemba.