+4 votes
in Operating Systems by (40.5k points)
I have to copy data from one file to another file. The input file has a header, whereas the output should not. Which Linux command should I use to skip the header while copying the data?

1 Answer

+3 votes
by (56.7k points)
selected by
 
Best answer

You can use the Linux command "tail" with the following option:

-n, --lines=[+]NUM  

The command will output the last NUM lines, instead of the last 10. If you use -n +NUM, it will output starting with line NUM.

My file "testfile.txt" contains the following data:

name    age     salary

aaa     23      2000

bbb     24      8721

ccc     27      5672

ddd     56      323

eee     34      9821

fff     28      4322

To output the last two lines, I will use the following command:

tail -n 2  testfile.txt

It will output the following:

eee 34 9821

fff 28 4322

To skip the header, I will use the following command. Instead of 2, I used +2, which means starting from the second record.

tail -n +2  testfile.txt

It will output the following:

aaa 23 2000

bbb 24 8721

ccc 27 5672

ddd 56 323

eee 34 9821

fff 28 4322

To copy the data from one file to another file skipping the header, I will use the following command:

tail -n +2  testfile.txt > testfile1.txt


...