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?
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?
I am not able to use the following command on my Mac (more specifically, in a bash shell running in a Terminal window on Mac OS X 10.7.5):
The command above generates the following error message:
It appears negative values for the -n parameter are not accepted in the Mac OS X version of head. However, you can use sed instead:
["tail -n +2 file2" appears to work as described above on Mac OS X]
will print all lines in
file1
except the last line.will print all lines in
file2
except the first line.Combine the two as:
you can use:
head shows the first lines of a file, the param -n shows the first n lines of the file, but if you put a - before the number, it shows all the file except the last n lines.
tail is analog..
for better reference:
man
head
man
tail
Explanation as requested:
FNR is the current record number of the current file being processed. At the start of each iteration, the line is stored in "p" variable, but it is not printed out, yet. As the record number reaches 3, the variable "p" is printed out, which contains record 2. This means the 2nd line. As awk reaches end of file, the last line is not printed out and then it goes to the next file.
The following transcript shows how to acheive this:
Giving the
head
command a negative count goes up to that far from the end. Similarly, a count starting with+
causestail
to start at that line rather than a certain number of lines from the end.