Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find line then evaluate text on next line, print when condition is met
# 1  
Old 02-07-2018
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,
Code:
>  <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 what follows InChI=.

I tried in awk,
Code:
$ cat ./filename | awk 'F = 1 { if($0 ~ "InChI=") { print $0; F = 0 } } $0 ~ "<CompoundName>" { F = 1 } '

This is intended to mean that when $0 contains "<CompoundName>", F is set to 1. When F = 1 (the line following "<CompoundName>"), if the line contains "InChI=", the line is printed and F is reset.

This does not work and I get a print of every line that contains "InChI=".

What did I do wrong here? I would guess that grep is the better tool for this but I don't know grep well.

Thanks,

LMHmedchem
# 2  
Old 02-07-2018
Code:
F == 1

# 3  
Old 02-07-2018
Try also
Code:
awk '/<CompoundName>/ {getline TMP; if (TMP ~ /InChI=/) print TMP}' file

or even
Code:
awk '/<CompoundName>/ {getline; if (/InChI=/) print}' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-07-2018
You can also try with sed.
Code:
sed -n '/<CompoundName>/!d;n;/InChI=/p' infile

This User Gave Thanks to ctac_ For This Post:
# 5  
Old 02-07-2018
Quote:
Originally Posted by rdrtx1
Code:
F == 1

When changing to,

cat inputfile | awk 'F == 1 { if($0 ~ "InChI=") { print $0; F = 0 } } $0 ~ "<CompoundName>" { F = 1 } '

I get the same output running this as I do for F = 1. Do I have the braces wrong or something?

This following solutions works as far as tested,

sed -n '/<CompoundName>/!d;n;/InChI=/p' infile

awk '/<CompoundName>/ {getline TMP; if (TMP ~ /InChI=/) print TMP}' file

awk '/<CompoundName>/ {getline; if (/InChI=/) print}' file

Thanks for the help. I am surprised to not see a grep solution. I always think of grep as a way to find and print text.

LMHmedchem
# 6  
Old 02-07-2018
Quote:
Originally Posted by LMHmedchem
When changing to,

cat inputfile | awk 'F == 1 { if($0 ~ "InChI=") { print $0; F = 0 } } $0 ~ "<CompoundName>" { F = 1 } '

I get the same output running this as I do for F = 1. Do I have the braces wrong or something?
.
.
.
Might be due to spurious F , mayhap set by a CompoundName way before. You should reset F after each line processed, like
Code:
awk 'F == 1 && /InChI=/; { F = 0 } $0 ~ "<CompoundName>" { F = 1 } ' file
InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1

Quote:
I am surprised to not see a grep solution. I always think of grep as a way to find and print text.
grep prints lines FOUND (plus context, that is); it CAN'T print the following line only... If your grep allows for the -A option, try
Code:
grep -A1 CompoundName file | grep InChI

(implying the InChI is not contained in the CompoundName lines)

Last edited by RudiC; 02-07-2018 at 04:54 PM..

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. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

3. 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

4. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

9. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

10. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

Featured Tech Videos