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

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay