Question: add header with awk
0
gravatar for Sam
10 weeks ago by
Sam70
Sam70 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 • 282 views
ADD COMMENTlink modified 10 weeks ago by toralmanvar530 • written 10 weeks ago by Sam70
3
gravatar for WouterDeCoster
10 weeks ago by
Belgium
WouterDeCoster32k 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 10 weeks ago • written 10 weeks ago by WouterDeCoster32k
2
gravatar for cpad0112
10 weeks ago by
cpad01129.0k
India
cpad01129.0k 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 10 weeks ago • written 10 weeks ago by cpad01129.0k
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 10 weeks ago by cpad01129.0k
1
gravatar for RamRS
10 weeks ago by
RamRS17k
Houston, TX
RamRS17k 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 10 weeks ago • written 10 weeks ago by RamRS17k
1
gravatar for toralmanvar
10 weeks ago by
toralmanvar530
toralmanvar530 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 10 weeks ago by toralmanvar530
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 10 weeks ago by RamRS17k

yes, you are very much right.

ADD REPLYlink written 10 weeks ago by toralmanvar530
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: 697 users visited in the last hour