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.
# 1  
Old 05-23-2016
Grep for text between twp strings for multiple occurances.

I need to get text between two strings <app-deployment file=" and </app-deployment> as it appears more than once in the file then how can i store the text between each occurrence in a separate file ?

Quote:
more deploy.tmp
<app-deployment file="file1">
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
<app-deployment file="file2">
<name>Security</name>
<target>CS2</target>
<module-type>ear</module-type>
</app-deployment>
Thus, i need the below to go in found1.tmp

Quote:
<app-deployment file="file1">
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
and the below to go in found2.tmp

Quote:
<app-deployment file="file2">
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
Reference: Search between two strings for multiple occurances
# 2  
Old 05-23-2016
Hello mohtashims,

Could you please try following and let me know if this helps you.
Code:
awk '/<app-deployment file/{Q++} {E=E?E ORS $0:$0} /<\/app-deployment>/{print E > "found"Q".tmp";E=""}'   Input_file

Thanks,
R. Singh
# 3  
Old 05-23-2016
Quote:
Originally Posted by RavinderSingh13
Hello mohtashims,

Could you please try following and let me know if this helps you.
Code:
awk '/<app-deployment file/{Q++} {E=E?E ORS $0:$0} /<\/app-deployment>/{print E > "found"Q".tmp";E=""}'   Input_file

Thanks,
R. Singh
I am passing the Input_file as an variable entry and also changing the file name how it is saved.

But it is failing
below is what i m doing

Code:
ls *.xml | while IFS= read -r entry; do
echo "ENTRY:"$entry
awk '/<foreign-server name/{Q++} {E=E?E ORS $0:$0} /<\/foreign-server>/{print E > $entry"_found"Q".tmp";E=""}'  $entry

# 4  
Old 05-23-2016
Hello mohtashims,

Could you please try following and let me know if this helps you.
Code:
for file in *.xml
do
     awk '/<app-deployment file/{Q++} {E=E?E ORS $0:$0} /<\/app-deployment>/{print E > "found"Q".tmp";E=""}'  Input_file
done

But problem here is you haven't told us like multiple xml files are there which you want to parse. Now if even you run above command finally only 2 files will be created. So in case you want to append all the output to files then change a little to above command.
Code:
for file in *.xml
do
     awk '/<app-deployment file/{Q++} {E=E?E ORS $0:$0} /<\/app-deployment>/{print E >> "found"Q".tmp";E=""}'  Input_file
done

Let me know if your requirement is different or you have additional conditions too with it. I hope this helps.

Thanks,
R. Singh
# 5  
Old 05-23-2016
Thank you for responding. You have not addressed the complete issue i am facing.

Let me elaborate.

I read all *.tmp file from a folder in a do-while loop in the variable called entry.

deploy.tmp in the OP is just one such file out of the many.

Now i want the parsing to happen on each of the files one by one and should be saved as below (considering that the loop picked deploy.tmp first)

Quote:
deploy_found1.tmp
deploy_found2.tmp
...
and like wise depending of the number of matches found for text between <app-deployment file=" and </app-deployment>
Likewise for update.tmp in entry variable i should be update.found1.tmp update.found2.tmp and likewise.

Can you help ?
# 6  
Old 05-23-2016
Quote:
Originally Posted by mohtashims
Thank you for responding. You have not addressed the complete issue i am facing.
Let me elaborate.
I read all *.tmp file from a folder in a do-while loop in the variable called entry.
deploy.tmp in the OP is just one such file out of the many.
Now i want the parsing to happen on each of the files one by one and should be saved as below (considering that the loop picked deploy.tmp first)
and like wise depending of the number of matches found for text between <app-deployment file=" and </app-deployment>
Likewise for update.tmp in entry variable i should be update.found1.tmp update.found2.tmp and likewise.
Can you help ?
Hello mohtashims,

I apologies for not understanding your requirement completely. I have tried my best to address the issue as per your information provided into your posts, I want to request you please let us know your complete requirement(in spite of giving in bits and pieces). In my previous post I have told you myself like output will be overwritten into same file, because we are reading multiple xml files by a loop.

Let me ask you some questions here for solving this problem.

i- You have mentioned previous post you are having multiple xmls, but in your previous post you have shown us *.tmp, how these 2 are related?
ii- If you have multiple files(xmls) and you have made a single big output file then did you try to consider my first post's solution?
iii- Please re-phrase your problem with sample Input_file and expected Output_file again in case you think information provided is confusing people(At least I am confuse). Please help us here to help you.

Thanks,
R. Singh
# 7  
Old 05-23-2016
i- You have mentioned previous post you are having multiple xmls, but in your previous post you have shown us *.tmp, how these 2 are related?

The are the same i overlooked .xml for .tmp

ii- If you have multiple files(xmls) and you have made a single big output file then did you try to consider my first post's solution?

I have multiple .tmp files but each .tmp file which will act as Input_file file in the do-while loop and then it needs to be parsed with the two string mentioned in the OP.

For each match found it should save each parsed data in a new file
thus for two match found between strings it should save output file with this name.

Input_file_found1.tmp
Input_file_found2.tmp.

Check my previous post for the example.

iii- Please re-phrase your problem with sample Input_file and expected Output_file again in case you think information provided is confusing people(At least I am confuse). Please help us here to help you.

Sample input file is the deploy.tmp in the OP but like i said i have many files like the deploy.tmp which i will be reading one by one and using yr suggestions to parse them.

In the OP the output filename should now be deploy.tmp_found1.tmp and deploy.tmp_found2.tmp and likewise depending on the number of matches found and deploy.tmp being the Input file name what will change as we read files from that folder in the do-while.

I hope i hv explained my requirement.

Last edited by mohtashims; 05-23-2016 at 11:35 AM..
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