Grep command on multiple line


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep command on multiple line
# 1  
Old 11-18-2018
Grep command on multiple line

Hi All,

I have some xml files and I need to find out all xml where one specific type of pattern is available..

Pattern 1
==========
Code:
<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT>without Any Mapping</COMMENT>  
</MAP>

Pattern 2
==========
Code:
<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT></COMMENT>  
</MAP>


Pattern 3
=============
Code:
<MAP TIMEOUT="" MODE="STANDALONE">  
  <COMMENT>Mapp step with mapping</COMMENT>  
  <MAPTARGET>      
  //Import mapping steps
  </MAPTARGET>  
  <MAPSOURCE>  
  //Import mapping steps
  </MAPSOURCE>  
<MAPCOPY FROM="/Package;1;0" TO="/node;1;0">  
</MAPCOPY>  
</MAP>

If you notice inpattern 1 & 2 inside <MAP>tag only <COMMENT> is there but in pattern 3 there is lots of other steps are there. I need to find all xml where its either match pattern 1 or 2.

I have tried with below syntex but its not working

Code:
find ./packages -type f -name flow.xml | xargs grep -l '.*<MAP TIMEOUT="[a-zA-Z0-9$]*" MODE="STANDALONE"><COMMENT>"[a-zA-Z0-9!@#$%^&*(),.?":{}|<>\n\[\] ]*"</COMMENT></MAP>'

Please see if anyone can help on this?

Thanks
Baharul Islam
# 2  
Old 11-18-2018
To do what you want you will need
Code:
grep -P

which uses the PCRE library, and is found, AFAIK, only by default on some Linux distributions. What EXACT version of UNIX/Linux do you have? What shell?

Code:
uname -a

Otherwise you probably should consider awk
# 3  
Old 11-24-2018
Removed advertisement and added code tags

Here is a solution which is very useful having seen this concept:

if 'abc' and 'efg' can be on the same line:

Code:
grep -zl 'abc.*efg' <your list of files>

if 'abc' and 'efg' must be on different lines:

Code:
grep -Pzl '(?s)abc.*\n.*efg' <your list of files>

Moderator's Comments:
Mod Comment Please do not put advertisements in your post and do use code tags for code and data

Furthermore, i do not see how this mechanism would help here - and this is not the first time i see you doing this. Please post solutions to the presented problems, not some rather tangential interesting-to-know type stuff.

Last edited by bakunin; 11-24-2018 at 03:22 AM..
# 4  
Old 11-24-2018
Hi, try something like:
Code:
find ./packages -type f -name flow.xml -exec awk '/^<MAP .*<\/COMMENT>.*<\/MAP>$/{print FILENAME}' RS= {} +





--
or if there can be empty lines in the file, use a dummy record separator to force awk to read the file at once and test for a newline at the end..
Code:
find . -type f -name 'infile6584*' -exec awk '/^<MAP .*<\/COMMENT>.*<\/MAP>\n$/{print FILENAME}' RS='§' {} +


Last edited by Scrutinizer; 11-24-2018 at 07:52 AM..
# 5  
Old 11-26-2018
You might want to use sed instead. Once you find STANDALONE, you can append the next line and look at it. This is an example of what your sed control file should look like:

Code:
/STANDALONE/ {
                                  N
                                  /with mapping/ {
                                   ...insert your code here ...
                                         }
                                   }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

3. Shell Programming and Scripting

Multiple Results of Grep in one Line/String?

Hello, I have a Textfile sees like this "Word1":aksdfjaksdf "Word2":askdfjalsdkfdlsjfasldfj "This is Word3":asdfkjalskdfj what i need is a string which sees like this Word1;Word2;This is Word3 Conclusion always the text within "" which is before the : i tried it with grep.... (10 Replies)
Discussion started by: SwordMaster
10 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. UNIX for Advanced & Expert Users

grep for a line in a file on multiple unix servers

Hi, I need to grep(look) for a line in filename named /tmp/Test on all 90 unix servers. Without logging to each servers manually and grep'ing for the line, is there a way to pull this info in output file thru' the ksh/sh/bash script. Can someone pl. help to create this script for me? Ex.... (3 Replies)
Discussion started by: Mike1234
3 Replies

8. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

9. Solaris

Multiple pattern on same line using grep

Hi, I would like to search multiple patterns on same line, i.e. all patterns must present on same line. Please suggest. Thanx (2 Replies)
Discussion started by: sanjay1979
2 Replies

10. UNIX for Dummies Questions & Answers

Multiple Line search using grep

Hi All, I am trying to search multiple lines in file using grep /sed.And i cant seem to make it work. The File looks like this 5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED 5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED... (6 Replies)
Discussion started by: pistachio
6 Replies
Login or Register to Ask a Question