The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract a substring using regular expression apraja Shell Programming and Scripting 2 03-04-2008 05:47 PM
how to extract a substring froma file morena UNIX for Dummies Questions & Answers 1 01-23-2008 07:22 PM
help for shell script of finding shortest substring from given string by user pankajd Shell Programming and Scripting 1 11-22-2007 12:27 PM
Using Awk in shell script to extract an index of a substring from a parent string sandeepms17 Shell Programming and Scripting 2 11-07-2007 07:44 AM
can i get a substring from a string? dell9 High Level Programming 4 11-06-2001 09:13 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-20-2006
girisha girisha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 5
command/script to extract a substring from a string

I have a long string "<ID type="Oid">{[ManagedElement(Key=rvpdn-nvl-mayo-17)][PhysicalRoot][Chassis][Slot(SlotNum=10000)][Module][Port(PortNumber=GigabitEthernet0/1)][PhysicalLayer][DataLinkLayer]}</ID>"

I need to extract "GigabitEthernet0/1" from the above string.
How can it be done?
  #2 (permalink)  
Old 09-20-2006
girisha girisha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 5
I can use cut -d')' -f3 | cut -d'=' -f2
Any other elegant way in unix?
  #3 (permalink)  
Old 09-20-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
Code:
long_string="
port_number=`echo "$long_string" | sed 's/.*PortNumber=\([^)]*\).*/\1/' `

Jean-Pierre.
  #4 (permalink)  
Old 09-20-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Using expr...
Code:
port_number=$(expr "$long_string" : ".*PortNumber=\([^)]*\).*")
  #5 (permalink)  
Old 09-21-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,533
alternatively in Python:

Code:
#!/usr/bin/python
import re
longstring = '"<ID type="Oid">{[ManagedElement(Key=rvpdn-nvl-mayo-17)][PhysicalRoot][Chassis][Slot(SlotNum=10000)][Module][Port(PortNumber=GigabitEthernet0/1)][PhysicalLayer][DataLinkLayer]}</ID>"'
get = re.findall(".*PortNumber=(.*)\)",longstring)
print get
  #6 (permalink)  
Old 09-21-2006
girisha girisha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 5
Thanks to all.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0