print the next line by searching with different patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print the next line by searching with different patterns
# 1  
Old 04-19-2012
print the next line by searching with different patterns

Hi,

I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search.

Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which are present in '.txt' file.

the logic which i used is
Code:
  while read line
  do 
    awk -f '/$line/ {for(j=1; j<=1; j++) {getline; print}}' sample.xml >> output.txt
  done< values.txt

for the above logic i am getting error as

Code:
awk -f '/$line/ {for(j=1; j<=1; j++) {getline; print}}' sample.xml
awk: fatal: can't open source file `/$line/ {for(j=1; j<=1; j++) {getline; print}}' for reading (No such file or directory)

in this logic, the value is reading by the variable 'line' and it is not evaluating in the awk cmd.



Can anyone advise on this?

Last edited by Franklin52; 04-19-2012 at 06:49 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 04-19-2012
if your fgrep supports -A option, then

Code:
 
fgrep -f values.txt -A 2 sample.xml

your awk command correction

Code:
awk -v l="$line" '$0~l{print;getline; print}' sample.xml

# 3  
Old 04-23-2012
Thanq raj !!

I corrected my awk command, but again i am getting the error as

Code:
awk -v $'l=SharedResources/JMS/username\r' '$0~l{print;getline; print}' ./sample.xml

I didnt understand the flow of awk command which you corrected.

And also i tried with the fgrep command, i am not getting any output and error as well.
can you please help me on this...
Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them
# 4  
Old 04-23-2012
The syntax should be
Code:
-v l='some extended regular expression'

# 5  
Old 04-23-2012
Actually in my .txt file i am having the values in the below format,

'abc/asd/zxw'

with these type of patterns, i have to search in .xml file and print the consecutive line.
# 6  
Old 04-23-2012
Did you try to use l='abc/asd/zxw' ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

2. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

3. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

6. Shell Programming and Scripting

How to print the next line by searching with different patterns in AIX server?

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which are... (4 Replies)
Discussion started by: tejastrikez
4 Replies

7. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

8. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. UNIX for Dummies Questions & Answers

searching for two or more patterns in a line

how can I search for two or more patterns in one line using grep? for example if I want to show only the lines that have patterns "abc" and "123"? and what if I want to show only the lines that have either "abc" or "123" in them? any hint apprecited (4 Replies)
Discussion started by: metalwarrior
4 Replies
Login or Register to Ask a Question