DEV Community

Peter Cooper
Peter Cooper

Posted on

Print Files But Without the First Line at the Terminal

I had a bunch of CSV files I wanted to join together and they all had the same 'header' line at the start. I didn't want the header line repeating in my concatenation of the files so I wanted to get all except the first line for my concatenation process (using cp).

Turns out there are several ways to do this. Let's say you have a file a.txt containing such:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

And your goal is to get:

2
3
4
5
Enter fullscreen mode Exit fullscreen mode

You can't use tail in the standard way as you might not know how long the file is. But you can use it with a special + number to specify that tail is to begin on a certain line, like so:

tail -n +2 a.txt
Enter fullscreen mode Exit fullscreen mode

You can also use awk to get the job done by specifying that it can return any lines where the line number count is larger than 1:

awk 'NR>1' a.txt
Enter fullscreen mode Exit fullscreen mode

Or you could use sed to delete the first line before displaying the file:

sed '1d' a.txt
Enter fullscreen mode Exit fullscreen mode

Each approach has various pros and cons mostly around which one you can remember in the moment or if you want to use awk or sed in other ways to make other adjustments, but it's handy to have options here.

To conclude, I took my CSV files and joined them like so:

cp 1.csv all.csv
tail -n +2 2.csv >> all.csv
tail -n +2 3.csv >> all.csv
tail -n +2 4.csv >> all.csv
Enter fullscreen mode Exit fullscreen mode

There are smarter ways to do this all in one go, but I only had a few files to do anyway! :-)

Top comments (0)