Question: add header with awk
0
gravatar for Sam
4 months ago by
Sam80
Sam80 wrote:

Dear All

I filtered a tab file with this command

awk -F'\t' '$15~/NO/' famliy.txt

but I want add header of input file to the out file with filtering command (one line command).

Thanks

awk • 446 views
ADD COMMENTlink modified 4 months ago by toralmanvar650 • written 4 months ago by Sam80
3
gravatar for WouterDeCoster
4 months ago by
Belgium
WouterDeCoster34k wrote:

Assuming the header is the first line of the file:

cat <(head -n 1 famliy.txt) <(awk -F'\t' '$15~/NO/' famliy.txt) > output
ADD COMMENTlink modified 4 months ago • written 4 months ago by WouterDeCoster34k
2
gravatar for cpad0112
4 months ago by
cpad01129.9k
India
cpad01129.9k wrote:
$ cat secret_family.txt 
header  header1
No  NO
No  Yes
no  no
yes No
yes NO

$ awk -F'\t'  'NR == 1 {print;next} $2 ~/NO/ ' secret_family.txt
header  header1
No  NO
yes NO

*if header doesn't contain "NO",  
$ awk -F'\t'  'NR == 1 {print} $2 ~/NO/ ' secret_family.txt

$ awk -F'\t'  'NR == 1 {print;next} NR>2 && $2 ~/NO/ ' secret_family.txt
header  header1
yes NO

$ awk -F'\t'  '{IGNORECASE=1};NR == 1 {print;next} NR>2 && $2 ~/NO/ ' secret_family.txt
header  header1
no  no
yes No
yes NO
ADD COMMENTlink modified 4 months ago • written 4 months ago by cpad01129.9k
1

same thing can be done with ebay tsv-utils input:

$ cat secret_family.txt 
header  header1
No  NO
No  Yes
no  no
yes No
yes NO

case sensitive filtering of 2nd column with header:

$ tsv-filter -H --str-eq 2:NO secret_family.txt 
header  header1
No  NO
yes NO

case insensitive filtering of 2nd column with header:

$ tsv-filter -H --istr-eq 2:NO secret_family.txt 
header  header1
No  NO
no  no
yes No
yes NO
ADD REPLYlink written 4 months ago by cpad01129.9k
1
gravatar for RamRS
4 months ago by
RamRS19k
Houston, TX
RamRS19k wrote:

Hard-code it in using a conditional on NR or combine this command with a separate preceding head command. Also, please explain how this is related to bioinformatics.

EDIT: More information.

awk has builtin variables, one of which is NR (the current record-number). This can be used as a proxy for line-number when the record separator is a new line. If your header is of constant length and in the same position at the top of the file, you can effectively use line numbers to write conditions. awk 'NR<3 { print }' in_file would print the first 2 lines of the file in_file. This can also be achieved with the command head -n 2 in_file.

ADD COMMENTlink modified 4 months ago • written 4 months ago by RamRS19k
1
gravatar for toralmanvar
4 months ago by
toralmanvar650
toralmanvar650 wrote:

I am not sure about awk, but then you can use below mentioned sed command followed by your command to add header to your new file generated. This consider the fact that your header is the 1st line of your file "family.txt" file.

sed '1q;d' famliy.txt >famliy_R1.txt | awk -F'\t' '$15~/NO/' famliy.txt >>famliy_R1.txt
ADD COMMENTlink written 4 months ago by toralmanvar650
1

What's the reasoning behind using the pipe here? Wouldn't a ; or a && be better suited given that no output piping occurs?

ADD REPLYlink written 4 months ago by RamRS19k

yes, you are very much right.

ADD REPLYlink written 4 months ago by toralmanvar650
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1423 users visited in the last hour