Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File1 + (File2 - first line) > File3

Tags:

unix

sed

cat

I have two csv/text files that I'd like to join. Both contain the same first line. I'm trying to figure out how to use sed and cat to produce a merged file, but with only one copy of the first line. And I'm having a time with syntax. Any help would be greatly appreciated :-D! Thanks, Andrew

like image 219
Andrew O'Dell Avatar asked Aug 27 '12 02:08

Andrew O'Dell


3 Answers

Another option with awk:

awk 'NR==FNR || FNR>1' file1.txt file2.txt .. fileN.txt

This prints all lines in the first file, OR any line in subsequent files after the first line.

like image 162
ghoti Avatar answered Nov 25 '22 17:11

ghoti


This will combine files data1.txt and data2.txt in file merged.txt, skipping the first line from data2.txt. It uses awk if you are ok with it:

(cat data1.txt; awk 'NR>1' data2.txt) > merged.txt

awk appends all lines with line number > 1 from file data2.txt to file merged.txt.

NR is a built-in awk variable that stands for the current line number of the file being processed. If the Boolean expression NR > 1 is true, awk prints the line implicitly.

If you didn't care about keeping data1.txt intact, you could just append your 2nd file (minus its first line) and reduce to just this:

awk 'NR>1' data2.txt >> data1.txt
like image 32
Levon Avatar answered Nov 25 '22 17:11

Levon


I'd say the most straightforward solution is:

( cat file1.txt ; tail -n +2 file2.txt ) > file3.txt

It has the advantage of stating clearly just what you're doing: print the entire first file, then print all but the first line of the second file, writing the output to the third file.

like image 28
Keith Thompson Avatar answered Nov 25 '22 16:11

Keith Thompson