How to Truncate a File from Command Line on Mac or Linux

screen-shot-2016-10-24-at-10-13-13-amI often get data files with a ton of data, but I only need the first few lines. It’s usually because I’m building out some test data for code I’m writing. I don’t need to create 2,000 accounts to run my code.

Here’s how I figured out how to truncate a file from the command-line on Mac.

sed -i '' '161,$ d' data.csv 

You don’t need the -i parameter ” if you’re on Linux.

sed -i '161,$ d' data.csv

The -i stands for in-place. The 161 is the line number to begin truncation. The $ means, truncate to the end of the file. Obviously, data.csv is the data file.

Share Button