Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cat two files after each other but omit the last/first line respectively?

I have two files I want to cat together. However, the last line of the first file and the first line of the last file should be omitted.

I am sure this can be done in a UNIX shell (or rather, Cygwin). But how?

like image 466
bastibe Avatar asked Aug 30 '10 12:08

bastibe


People also ask

How do you skip the first line while reading a file in Unix?

The NR variable indicates the number of records in a file. The following `awk` command uses the NR variable to skip the first line of a file. The value of NR is 1 for the first line. The following command will print lines for which the NR value is greater than 1.

What is cat filename << EOF?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

How do you overwrite in cat command?

The key takeaway from this section is that we use cat > FILENAME to create or overwrite a file. Additionally, we can use cat >> FILENAME to append to a file that's already there. Then after typing in the text we want we use CTRL + D to exit the editor, return to the command line, and create the file.


1 Answers

$ head --lines=-1 file1 > res
$ tail --lines=+2 file2 >> res
like image 181
Frank Avatar answered Oct 29 '22 12:10

Frank