I have a comma separated file with multiple fields.
Example:
candidate1,12,56,ATTG
candidate2,45,90,ATTG
candidate3,90,140,ATTG
candidate4,2,36,AGCT
So, does anyone know the answer?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI have a comma separated file with multiple fields.
Example:
candidate1,12,56,ATTG
candidate2,45,90,ATTG
candidate3,90,140,ATTG
candidate4,2,36,AGCT
So, does anyone know the answer?
Say you need to print unique lines based on column 1 (candidate1
, candidate2
etc). Note that this prints the first instance of a line with candidate1
etc. To print unique lines based on column 2, change $1
to $2
below.
awk -F, '!($1 in arr){print} {arr[$1]++}' file_name
If all the fields are comma separated like this one
1, candidate1,12,56,ATTG
2, candidate2,45,90,ATTG
3, candidate3,90,140,ATTG
4, candidate4,2,36,AGCT
Then you do something like this one
$ grep -E 'candidate1|candidate3' file_name | awk -F, '{print $0}' 1, candidate1,12,56,ATTG 3, candidate3,90,140,ATTG
another example for a single line
$ cat filename | egrep 'candidate3' | awk -F, '{print $0}' 3, candidate3,90,140,ATTG
awk
taking comma as default since it certainly does not on my system and I have never heard of it doing so. Perhaps it does for your awk
implementation but is is not a general rule.
awk
is doing nothing besides print the line. You are using grep
to find presence in the line, but the question is about particular column values (which awk
is well suited for).
awk
supposed to be doing? It does not change the output of grep
in any way. In any case, you are not looking for anything unique, you are just printing ALL lines that contain "candidate3", combined with a useless use of gawk and a useless use of cat. Your solution is basically grep 'candidate1\|candidate3'
.
90
from column 3? If you replaced your regex with "90" you would get lines 2 and 3.