'Ignore header when performing awk command - not working
I know several questions have been asked about ignoring the header using the awk command, but I have tried a bunch of the suggested methods and it still isn't working for me.
I am using awk to cut characters from a string, but the output file is being generated without the header, even though the header doesn't contain the characters that I am trying to cut.
Here is my command:
cat input | awk -F '|' '{print$2}' > output
This command does what I need, but my header rows are left empty. I am trying to print everything after the "|" symbol, but I want to keep the headers in my input file (which involves the first two rows). I have tried using NR>2, but to no avail. Maybe I am not placing the ignore header command in the right place. Any help is greatly appreciated. Thank you!!
Here is a sample of the input:
Total Number of Mapped Reads = XXXX
Genome Final Guess Final Hit
ACCN2|Bacterial Species 5.4 3.5
ACCN4|Bacterial Species 2.6 8.7
Desired Output:
Total Number of Mapped Reads = XXXX
Genome Final Guess Final Hit
Bacterial Species 5.4 3.5
Bacterial Species 2.6 8.7
Solution 1:[1]
Your sample command prints the second field of | separated file. SInce the first 2 records only have 1 field, nothing gets printed with $2
, instead print the last field $NF
:
$ awk -F\| '{print $NF}' file
Total Number of Mapped Reads = XXXX
Genome Final Guess Final Hit
Bacterial Species 5.4 3.5
Bacterial Species 2.6 8.7
Solution 2:[2]
You can use tail to skip a specific number of header lines here is an example:
command | awk '{print $1}' | tail +2
this will skip the first 2 lines after performing awk on the first field of the command result.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | James Brown |
Solution 2 | Affes Salem |