awk - print when condition is met

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk - print when condition is met
# 1  
Old 06-03-2017
awk - print when condition is met

I have a file.txt containing the following:

Code:
Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT
  
 Length=100
                                                                       Score     E
 Sequences producing significant alignments:                          (Bits)  Value
  
   database                                                            176     3e-45
  
  
 > database
 Length=4312079
  
  Score = 176 bits (194),  Expect = 3e-45
  Identities = 99/100 (99%), Gaps = 0/100 (0%)
  Strand=Plus/Plus
  
 Query  1       CACGCCCTTTGCTTGGCCCTTGGGGATGTGGCAGCTGATTATCCCGATAGGCGACCAGGA  60
                |||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||
 Sbjct  778226  CACGCCCTTTGCTTGGCCCTTGGGGATGTGGCAGCTGATTATCCCGGTAGGCGACCAGGA  778285
  
 Query  61      GTCTCGCAAGCTCCTGCCTTTGCCCCTCAGTCCATTGGAG  100
                ||||||||||||||||||||||||||||||||||||||||
 Sbjct  778286  GTCTCGCAAGCTCCTGCCTTTGCCCCTCAGTCCATTGGAG  778325
 
  
  Score = 26.5 bits (28),  Expect = 3.5
  Identities = 14/14 (100%), Gaps = 0/14 (0%)
  Strand=Plus/Minus
  
 Query  22       GGGGATGTGGCAGC  35
                 ||||||||||||||
 Sbjct  1898269  GGGGATGTGGCAGC  1898256

now I want to use awk to print the following lines (please see the condition below)
Code:
Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT
Sbjct  778226  CACGCCCTTTGCTTGGCCCTTGGGGATGTGGCAGCTGATTATCCCGGTAGGCGACCAGGA  778285

but only, if this line
Code:
 Identities = 99/100 (99%), Gaps = 0/100 (0%)

meets the condition, that Identities should be >50 (so I am only interesting in the part Identities = 99)

This is my command for printing the second line (it is always the 5th line below the line with Identities):
Code:
awk 'c&&!--c;/Identities =/ && ($3+0)>=50 {print q, c=5}' file.txt

the first line in the example output should only be printed, if the condition is met.

I am also able to print the first line only, without a condition:
Code:
awk '/Query= / {print $0} file.txt

but now my idea is to combine these to commands, so that the first line and the second line is only printed if the condition is met. I thought of putting the first line into a variable inside awk, and if the condition is met, print this first line and the second line.
I could come up with this:
Code:
awk '/Query= /{q=$0}' file.txt | awk 'c&&!--c;/Identities =/ && ($3+0)>=50 {print q, c=5}' file.txt

but it does not work as planned.
Any help is appreciated!

Last edited by tons92; 06-03-2017 at 03:36 PM..
# 2  
Old 06-03-2017
Defining a variable (q) in one awk script and using that variable in a different awk script can't work. But, you can easily combine your code into a single awk script:
Code:
awk '/Query= /{q=$0} c&&!--c;/Identities =/ && ($3+0)>=50 {print q, c=5}' file.txt

and get what I think you want:
Code:
Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT 5
 Sbjct  778226  CACGCCCTTTGCTTGGCCCTTGGGGATGTGGCAGCTGATTATCCCGGTAGGCGACCAGGA  778285

You might also consider trying:
Code:
awk '
$1 == "Query=" {
	q = $0
	next
}
$1 == "Identities" && ($3 + 0) > 50 {
	print q
	printSbjct = 1
	next
}
printSbjct && $1 == "Sbjct" {
	print
	printSbjct = 0
	next
}' file.txt

which also produces the same output. It is a little be longer, but I find it easier to read and understand.

P.S. Note that your specification said > 50, but your code used >=50. I copied the >= when I combined your code, but I used what you specified in your description in the code I wrote.

Last edited by Don Cragun; 06-03-2017 at 04:29 PM.. Reason: Add PS.
# 3  
Old 06-03-2017
thank you! that's what I was looking for. and it should have been >=50
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

awk to update file with numerical difference if condition is met

In the file1 below if $9 and $12 are . (dot) then the value in $8 of file1 is used as a key (exact match) to lookup in each $2 of file2, when a match is found then the value of $4 in file1 is used to look for a range match within +/- 50 using the values in $4 and after in file2. The number of... (9 Replies)
Discussion started by: cmccabe
9 Replies

4. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

6. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

7. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

8. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

9. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

10. Shell Programming and Scripting

sed/awk to update 1st column if condition met

Hi, I am trying to update the 1st column of a file but only if it contains a char here is an example of my file 1111aaa 9999 textaaa 22222bbb 9999 textbbb 3333 9999 textccc 444ddd 9999 textddd i would like the output to remove any characters () from... (5 Replies)
Discussion started by: plennon
5 Replies
Login or Register to Ask a Question