Grep between two strings in shell

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Grep between two strings in shell
# 1  
Old 07-04-2013
RedHat Grep between two strings in shell

Code:
<cisco:subname>
         <cisco:sptp>Cisco PortA Series</cisco:sptp>
         <cisco:aliasNameList xsi:nil="true"/>
         <cisco: owner xsi:nil="true"/>
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>
       </cisco:subname>


in the above xml file how to get the data between <cisco:subportname> and </cisco:subportname>
Moderator's Comments:
Mod Comment Please use CODE tags when showing sample input, output, and code.

Last edited by Don Cragun; 07-04-2013 at 04:34 AM.. Reason: Add CODE and ICODE tags
# 2  
Old 07-04-2013
Try something like:
Code:
awk '
$1 == "</cisco:subportname>" {
        p = 0
        next
}
p
$1 == "<cisco:subportname>" {
        p = 1
}' file

If you are using a Solaris system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /usr/bin/awk or /bin/awk.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-04-2013
Quote:
Grep between two strings in shell


Code:
<cisco:subname> <cisco:sptp>Cisco PortA Series</cisco:sptp> <cisco:aliasNameList xsi:nil="true"/> <cisco: owner xsi:nil="true"/> <cisco:subportname> <cisco:cpt>Cisco SubPort B Series</cisco:cpt> <cisco:aliasNamesubList xsi:nil="true"/> <cisco:userLabel xsi:nil="true"/> </cisco:subportname> </cisco:subname>


in the above xml file how to get the data between <cisco:subportname> and </cisco:subportname>

Hello,

Kindly check with this too.

Code:
$ awk '/\<cisco:subportname\>/','/\<\/cisco:subportname\>/' File

Output will be as follows.


Code:
 
                      <cisco:subportname>
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>
               </cisco:subportname>

Thanks,
R. Singh

---------- Post updated at 03:52 AM ---------- Previous update was at 03:48 AM ----------

Quote:
Posted by Don

Try something like:


Code:
awk '$1 == "</cisco:subportname>" { p = 0 next}p$1 == "<cisco:subportname>" { p = 1}' file

If you are using a Solaris system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of /usr/bin/awk or /bin/awk .


That is excellent code Don, could you please explain me, as I am not able to see any if or print statement but it gives exactly same output.

I will be grateful to you.



Thanks,
R. Singh
# 4  
Old 07-04-2013
The code I gave you was not:
Code:
awk '$1 == "</cisco:subportname>" { p = 0 next}p$1 == "<cisco:subportname>" { p = 1}' file

It was:
Code:
awk '
$1 == "</cisco:subportname>" {
        p = 0
        next
}
p
$1 == "<cisco:subportname>" {
        p = 1
}' file

and the output is not the same. This script only prints the lines requested:
Code:
                 <cisco:cpt>Cisco SubPort B Series</cisco:cpt>
                 <cisco:aliasNamesubList xsi:nil="true"/>
                 <cisco:userLabel xsi:nil="true"/>

(The lines between the line containing <cisco:subportname> and the line containing </cisco:subportname>; not those two lines and the lines between them.)

The variable p is a flag that causes input lines to be printed when p is non-zero. The initial value of any awk variable that has never been set is 0 or an empty string (depending on context). The code in orange turns printing on for following lines when the string <cisco:subportname> is the first string on the line (after any leading whitespace characters).

The code in green turns printing off for that line and following lines when the string </cisco:subportname> is the first string on the line (after any leading whitespace characters).

The code in red prints the current input line if p is set to 1 and does nothing if p is set to 0 or is an empty string. (When a condition evaluates to true [any non-zero numeric value or non-empty string] and there is no action specified, the default action is to print the current input line.)

Last edited by Don Cragun; 07-04-2013 at 06:28 AM.. Reason: Fix typo
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Grep strings for different cases

Hi All, Good morning I have a below code which is working & getting expected output. the problem in this code is it is executing 3 if conditions, my requirement is suppose if first condition is success then it should print echo statement & exit from if condition else if the 1st if condition... (4 Replies)
Discussion started by: sam@sam
4 Replies

3. Shell Programming and Scripting

Grep for strings

Hi, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 How can I grep for the strings chrome and safari from a file, and if chrome print Chrome/40.0.2214.94 to a file and also count the number of times chrome is found? ... (4 Replies)
Discussion started by: cyberfrog
4 Replies

4. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

5. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

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

7. Shell Programming and Scripting

grep two strings in a file..

Hello All, I have a big file about 1000 lines. Now i am trying to grep a particular string and printing the lines from the string. say for example in 500th line i have the date as "Mon Wed 14 20:15:24 2010". now i in my case i need to grep the combination of the strings "Mon Wed 14" and the... (7 Replies)
Discussion started by: intiraju
7 Replies

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

9. Shell Programming and Scripting

want to grep only strings in a file?

Hai, Just want to print only alphanumeric in a file ex:- fdsdsklf#@^%$#hf output:- fdsdsklfhf plz, help me:o (5 Replies)
Discussion started by: balan_mca
5 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question