DEV Community

Peter Cooper
Peter Cooper

Posted on

1 1

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! :-)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more