Grep between two strings in shell | Unix Linux Forums | Red Hat

  Go Back    


Red Hat Red Hat is the world's leading open source technology solutions provider with offerings including Red Hat Enterprise Linux (RHEL), Fedora, open source applications, security and systems management, virtualization, and Services Oriented Architecture (SOA) solutions.

Grep between two strings in shell

Red Hat


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-04-2013
itsspy itsspy is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 4 July 2013, 3:17 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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:
Please use CODE tags when showing sample input, output, and code.

Last edited by Don Cragun; 07-04-2013 at 03:34 AM.. Reason: Add CODE and ICODE tags
Sponsored Links
    #2  
Old 07-04-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 1:27 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,645 Times in 1,396 Posts
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 .
The Following User Says Thank You to Don Cragun For This Useful Post:
RavinderSingh13 (07-04-2013)
Sponsored Links
    #3  
Old 07-04-2013
RavinderSingh13 RavinderSingh13 is offline
Registered User
 
Join Date: May 2013
Last Activity: 23 October 2014, 2:23 PM EDT
Location: Chennai
Posts: 740
Thanks: 206
Thanked 164 Times in 159 Posts
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
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 1:27 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,645 Times in 1,396 Posts
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 05:28 AM.. Reason: Fix typo
The Following User Says Thank You to Don Cragun For This Useful Post:
RavinderSingh13 (07-04-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can't grep multiple strings cfjohnsn Shell Programming and Scripting 3 11-21-2012 03:43 PM
grep two strings in a file.. intiraju Shell Programming and Scripting 7 02-23-2010 09:10 AM
Grep Multiple Strings durgaprasad Shell Programming and Scripting 10 06-26-2009 06:38 AM
grep strings of a certain length angela.perez7 Shell Programming and Scripting 2 02-14-2009 02:39 PM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 11:58 AM



All times are GMT -4. The time now is 04:29 AM.