Sponsored Content
Operating Systems Linux Red Hat Grep between two strings in shell Post 302829109 by RavinderSingh13 on Thursday 4th of July 2013 04:52:43 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
POE::Filter::Grep(3pm)					User Contributed Perl Documentation				    POE::Filter::Grep(3pm)

NAME
POE::Filter::Grep - select or remove items based on simple rules SYNOPSIS
#!perl use POE qw( Wheel::FollowTail Filter::Line Filter::Grep Filter::Stackable ); POE::Session->create( inline_states => { _start => sub { my $parse_input_as_lines = POE::Filter::Line->new(); my $select_sudo_log_lines = POE::Filter::Grep->new( Put => sub { 1 }, Get => sub { my $input = shift; return $input =~ /sudo[d+]/i; }, ); my $filter_stack = POE::Filter::Stackable->new( Filters => [ $parse_input_as_lines, # first on get, last on put $select_sudo_log_lines, # first on put, last on get ] ); $_[HEAP]{tailor} = POE::Wheel::FollowTail->new( Filename => "/var/log/system.log", InputEvent => "got_log_line", Filter => $filter_stack, ); }, got_log_line => sub { print "Log: $_[ARG0] "; } } ); POE::Kernel->run(); exit; DESCRIPTION
POE::Filter::Grep selects or removes items based on simple tests. It may be used to filter input, output, or both. This filter is named and modeled after Perl's built-in grep() function. POE::Filter::Grep is designed to be combined with other filters through POE::Filter::Stackable. In the "SYNOPSIS" example, a filter stack is created to parse logs as lines and remove all entries that don't pertain to a sudo process. (Or if your glass is half full, the stack only selects entries that DO mention sudo.) PUBLIC FILTER METHODS
In addition to the usual POE::Filter methods, POE::Filter::Grep also supports the following. new new() constructs a new POE::Filter::Grep object. It must either be called with a single Code parameter, or both a Put and a Get parameter. The values for Code, Put, and Get are code references that, when invoked, return true to select an item or false to reject it. A Code function will be used for both input and output, while Get and Put functions allow input and output to be filtered in different ways. The item in question will be passed as the function's sole parameter. sub reject_bidoofs { my $pokemon = shift; return 1 if $pokemon ne "bidoof"; return; } my $gotta_catch_nearly_all = POE::Filter::Grep->new( Code => &reject_bidoofs, ); Enforce read-only behavior: my $read_only = POE::Filter::Grep->new( Get => sub { 1 }, Put => sub { 0 }, ); modify modify() changes a POE::Filter::Grep object's behavior at run-time. It accepts the same parameters as new(), and it replaces the existing tests with new ones. # Don't give away our Dialgas. $gotta_catch_nearly_all->modify( Get => sub { 1 }, Put => sub { return shift() ne "dialga" }, ); SEE ALSO
POE::Filter for more information about filters in general. POE::Filter::Stackable for more details on stacking filters. BUGS
None known. AUTHORS &; COPYRIGHTS The Grep filter was contributed by Dieter Pearcey. Documentation is provided by Rocco Caputo. Please see the POE manpage for more information about authors and contributors. perl v5.14.2 2012-05-15 POE::Filter::Grep(3pm)
All times are GMT -4. The time now is 07:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy