0

Suppose that I have two files, each of them have header in the first line and records in the remaining lines. And I want to concatenate two files into one, but don't include header twice.

I tried the following commands while googling for the answer, (hence I may not cope in an optimal way).

cat awk 'NR!=1 {printf "%s\n", $1}' file2.csv >| file.csv

However, I got the following error.

cat: awk: No such file or directory
cat: NR!=1 {printf "%s\n",$1}: No such file or directory

It looks like cat recognized awk as files, not commands. I want the result of awk to be the content of files, so I also tried to pipe it to the argument of cat.

awk 'NR!=1 {printf "%s\n", $1}' file2.csv > cat file.csv

However, in this way, I got file cat, in which I got the result of awk...

So how can I solve it?

Thanks.

Blaszard
  • 30,954
  • 51
  • 153
  • 233

3 Answers3

1

You need some grouping:

{ 
    cat file1
    sed '1d' file2
} > file.csv

As one line

{ cat file1; sed '1d' file2; } > file.csv

The semicolon before the ending brace is required.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0
{cat file1; tail -n +2 file2} > out
Keith Flower
  • 4,032
  • 25
  • 16
0

Print first line from first file, then print line #2 to the end of any file

awk 'NR==1||FNR>1' file1 file2 (file3 file4 ..) > outfile
Hermann
  • 166
  • 2