Grep for text between twp strings for multiple occurances.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for text between twp strings for multiple occurances.
# 8  
Old 05-23-2016
Hello mohtashims,

Could you please try following and let me know how it goes from there then. It will put files names like for an exampleFile1found1.tmp and File1found2.tmp.
Code:
for file in *.xml
do
     awk '/<app-deployment file/{Q++} {E=E?E ORS $0:$0} /<\/app-deployment>/{print E > FILENAME"found"Q".tmp";E=""}'  $file
done

You could change for file in *.xml to for file in *.tmp etc as per your need.

Thanks,
R. Singh
# 9  
Old 05-23-2016
I fully second RavinderSingh13 saying that your specifications could be WAY clearer and more precise FROM THE START. Amongst other incertainties, your output file names have been specified to be

- found1.tmp
- deploy_found1.tmp
- update.found1.tmp
- deploy.tmp_found1.tmp
- Input_file_found1.tmp (might be a variation of No. 2)

Wouldn't it save YOUR time, too, making up your mind first and then posting?


How about
Code:
awk '
FNR == 1                {FCNT = 0
                        }
/<app-deployment/       {FN = FILENAME
                         sub ("\.", ".found" ++FCNT ".", FN)
                        }
/<app-deployment/,
/<\/app-deployment/     {print > FN
                        }
' *.tmp

cf *found*
file3.found1.tmp:
<app-deployment file="file1">
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
file3.found2.tmp:
<app-deployment file="file2">
<name>Security</name>
<target>CS2</target>
<module-type>ear</module-type>
</app-deployment>
file4.found1.tmp:
<app-deployment file="file3">
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
file4.found2.tmp:
<app-deployment file="file4">
<name>Security</name>
<target>CS2</target>
<module-type>ear</module-type>
</app-deployment>


Last edited by RudiC; 05-23-2016 at 03:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search between two strings for multiple occurances

i search between two strings viz <app-deployment> & </app-deployment> and save the contents in a new file using the code snippet below. sed -n "/<app-deployment/,/<\/app-deployment>/p" deploy.tmp >found1.tmpBut if the search string apprears more than once in the file then how can i store the... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Grep multiple strings in a file

Consider i have the below data in my log file. i want to grep using "Monday" and "Working" So the only output i expect is Can you help me with the grep query for Sun Sparc ? Usage: grep -hblcnsviw pattern file . . . (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Whether we can search multiple strings using or in grep -F

Hi, Whether we can search multiple strings using or in grep -F In Generally, grep -F "string1" "filename.txt" How to search for multiple string using grep -F as we using grep grep "string1\|string2" "filename.txt" Regards, Nanthagopal A (10 Replies)
Discussion started by: nanthagopal
10 Replies

4. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

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

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

8. Shell Programming and Scripting

How to get filename from the fullpath and how to grep multiple strings

Hi, New to shell scripting.... I have log file content as below: I have to count the number of occurences of ERROR or INFO Messages. So, I cut 5 th column and uniquly sorted and redirected it to new.txt file. But I want copy to S*/Filename and T*/Filename of respective ERROR or INFO... (5 Replies)
Discussion started by: Shirisha
5 Replies

9. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

10. Shell Programming and Scripting

Efficient way to grep multiple strings

I have a script which searches a huge log file for the existence of a specified string and if the string is not present i receive an alert mail. Here's an extract: STRING=$(grep 'warning' logfile | tail -1 | wc -l) if (( ${STRING} > 0 )); then print -- "---- Warning etc.... (3 Replies)
Discussion started by: Moxy
3 Replies
Login or Register to Ask a Question