Grep or awk a unique and specific word across many fields

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep or awk a unique and specific word across many fields
# 8  
Old 06-16-2017
You mean ONE or more homo AND NO hetero or NO homo AND ONE or more hetero? Then, yes, that's how you do it.
I have to admit, using >= 2 (or, in your second case, 1 ) would make the logics way clearer. I might adapt to that in the future...
This User Gave Thanks to RudiC For This Post:
# 9  
Old 06-16-2017
Does awk work in a range of columns where I can specify groups
for example: (group 1: ranging from 6-60 and group 2: from 61-100)
and my separation will be based on hetero in group 1 and homo in group 2 in the same string and vice versa. is this separation is achievable ?

As always I appreciate your efforts in making SCIENCE great again Smilie

Last edited by daashti; 06-16-2017 at 07:17 PM..
# 10  
Old 06-16-2017
Quote:
Originally Posted by daashti
Worked like a charm.

Just a question if I want to the separation to include equal or more than 1.

Do I have to modify the code to this:
Code:
awk '
        {split ("",  C)
         for (i=6; i<=NF; i++) C[$i]++
         CM = C["homo"]
         CR = C["hetero"]
        }
(CM > 0) && !CR ||
(CR > 0) && !CM ||
NR == 1
' file

Thanks
Yes, the code above should work. In this specific case (i.e., >0), you could further simplify the lines:
Code:
(CM > 0) && !CR ||
(CR > 0) && !CM ||
NR == 1

to:
Code:
CM && !CR ||
CR && !CM ||
NR == 1

And, as long as the only multi-character strings in your input file (other than in the header line) are the strings homo and hetero, and you want to create two output files you could also try:
Code:
NR == 1 || ((nhomo = gsub(/homo/, "&")) >= mincnt && !gsub(/hetero/, "&")) {
	print > "homo.txt"
}
NR == 1 || (gsub(/hetero/, "&") >= mincnt && !nhomo) {
	print > "hetero.txt"
}' file

Either of these suggested methods of processing your file could be modified to work on groups of fields. And, with the suggestions we have provided, you should be able to make an attempt to do so on your own. If you try it and can't quite get it to work, give us a clear specification of what you want to do, show us what you have tried to solve this update to the code we have provided, tell us where you're stuck, and we'll try to help you get to your goal.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 06-17-2017
Quote:
Originally Posted by daashti
Does awk work in a range of columns where I can specify groups
for example: (group 1: ranging from 6-60 and group 2: from 61-100)
.
.
.
Certainly. Logics might be more intricate. The for loop now runs from field 6 until the last field. Make it two loops, one from 6 to 60, the other from 61 to 100.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting max value of specific fields with awk

Hello All, Here is am trying to get maximum value of third field depending on first,second and fourth fields with awk command . delimeter is pipe(|) . input 0221|09|14.25|aaa 0221|09|44.27|aaa 0221|09|44.33|aaa 0221|09|44.53|bbb 0221|09|34.32|bbb 0221|09|37.13|bbb... (5 Replies)
Discussion started by: sayami00
5 Replies

2. Shell Programming and Scripting

awk to match keyword and return matches and unique fields

Trying to use awk to find a keyword and return the matches in the row, but also $1 and $2, which are the unique id's, but they only appear once. Thank you :). file name 31 Index Chromosomal Position Gene Inheritance 122 2106725 TSC2 AD 124 2115481 TSC2 AD 121 2105400 TSC2 AD... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

4. Shell Programming and Scripting

Print unique names in a specific column using awk

Is it possible to modify file like this. 1. Remove all the duplicate names in a define column i.e 4th col 2. Count the no.of unique names separated by ";" and print as a 5th col thanx in advance!! Q input c1 30 3 Eh2 c10 96 3 Frp c41 396 3 Ua5;Lop;Kol;Kol c62 2 30 Fmp;Fmp;Fmp ... (5 Replies)
Discussion started by: quincyjones
5 Replies

5. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

6. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

7. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

8. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. Shell Programming and Scripting

grep a word from a specific line

for example: searches only for single word for single word this is line three match=$(grep -n -e "single" data.txt) this command will stored "..... single ...... single" into search. how can i grep the single word just from line 2 only?? (3 Replies)
Discussion started by: blurboy
3 Replies
Login or Register to Ask a Question