Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two files (removing the first line of the second file) in a single line of unix command?

Tags:

file

merge

unix

sed

I am trying to create a new file (say file3) by stacking two files (file1 and file2) together. I need the entirety of file1 but want to exclude the first row of file2. What I have now is a two step process using head and sed.

cat file1.csv file2.csv > file3.csv
sed -i 1001d file3.csv

The last step requires me to find out the length of file1 before removing the line after so that it corresponds to the first line of file2. How do I combine these two lines into a single line of code? I tried this and it failed:

cat file1.csv sed 1d file2.csv > file3.csv
like image 530
Shan Abraham Avatar asked Oct 31 '25 03:10

Shan Abraham


2 Answers

You can use a compound statement like this:

{ cat file1.csv; sed '1d' file2.csv; } > file3.csv
like image 56
Mark Setchell Avatar answered Nov 02 '25 23:11

Mark Setchell


You can use process substitution, like:

cat file1.csv <(tail -n +2 file2.csv) > file3.csv
like image 43
Grisha Levit Avatar answered Nov 03 '25 00:11

Grisha Levit