Skip to main content
added 207 characters in body
Source Link
Ed Morton
  • 36k
  • 6
  • 25
  • 60
$ awk -F'\t' '(NR==1) || ($3=="Blue")' file
ID      Name    Eye Color
1       Bill    Blue
2       Sam     Blue
5       Ted     Blue

It sounds like what you're really trying to do, though, is create a new file per ID which, assuming the IDs are unique like in your example, would be:

awk -F'\t' '{ out="out_" $1 ".txt"; print > out; close(out) }' BindingDB_All.tsv

or if you want each output file to include the header:

awk -F'\t' '
    NR==1 { hdr=$0; next }
    { out="out_" $1 ".txt"; print hdr ORS $0 > out; close(out) } 
' BindingDB_All.tsv
$ awk -F'\t' '(NR==1) || ($3=="Blue")' file
ID      Name    Eye Color
1       Bill    Blue
2       Sam     Blue
5       Ted     Blue

It sounds like what you're really trying to do, though, is create a new file per ID which, assuming the IDs are unique, would be:

awk -F'\t' '{out="out_" $1 ".txt"; print > out; close(out)}' BindingDB_All.tsv
$ awk -F'\t' '(NR==1) || ($3=="Blue")' file
ID      Name    Eye Color
1       Bill    Blue
2       Sam     Blue
5       Ted     Blue

It sounds like what you're really trying to do, though, is create a new file per ID which, assuming the IDs are unique like in your example, would be:

awk -F'\t' '{ out="out_" $1 ".txt"; print > out; close(out) }' BindingDB_All.tsv

or if you want each output file to include the header:

awk -F'\t' '
    NR==1 { hdr=$0; next }
    { out="out_" $1 ".txt"; print hdr ORS $0 > out; close(out) } 
' BindingDB_All.tsv
Source Link
Ed Morton
  • 36k
  • 6
  • 25
  • 60

$ awk -F'\t' '(NR==1) || ($3=="Blue")' file
ID      Name    Eye Color
1       Bill    Blue
2       Sam     Blue
5       Ted     Blue

It sounds like what you're really trying to do, though, is create a new file per ID which, assuming the IDs are unique, would be:

awk -F'\t' '{out="out_" $1 ".txt"; print > out; close(out)}' BindingDB_All.tsv