The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse a string efficiently sandiego_coder Shell Programming and Scripting 4 05-13-2008 09:12 AM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 01:43 AM
Parse String in XML file racbern Shell Programming and Scripting 4 12-19-2007 08:26 AM
String parse question mnreferee Shell Programming and Scripting 5 03-06-2007 08:30 PM
parse a string variable methos Shell Programming and Scripting 3 10-18-2005 01:18 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Dec 2007
Posts: 21
Stumble this Post!
Parse String Using Sed

Hi,

I am wondering if there's a simpler way to extract the second occurrence of a word enclosed in [ ] that matches my search criteria.

Sample Input is as follows:

HTML Code:
[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra
[main] Error installing feature [fmav] - com.er.nms.cif.ist.NoMatchingUpgra
Based on the Input, I need to get the following output:
HTML Code:
fmx 
fmav

Here's my code:

Code:
while read data; do
echo $data | sed -e 's/\([.[a-z]*] \)//' | sed 's/[A-Za-z ]*//' | sed 's/-.*//' | sed 's/]//' | sed 's/\[//'
done < data.txt
Any help is greatly appreciated.


//racbern
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,599
Stumble this Post!
Code:
sed -e 's/.*\].*\[//' -e 's/\].*//' data.txt
The first substitution matches any string, closing square bracket (to skip past the first pair of square brackets), anything, followed by opening square bracket, and removes that. This should remove everything before your string. Then the second substitution replaces from closing square bracket to end of line with nothing.

As a side remark, you should always double quote any variables.
Reply With Quote
  #3 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Jul 2006
Posts: 189
Stumble this Post!
echo $str | sed -e 's/^\[.*\[//' -e 's/].*$//'
Reply With Quote
  #4 (permalink)  
Old 04-23-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Code:
echo '[main] Error installing feature [fmx] - com.er.nms.cif.ist.NoMatchingUpgra' | nawk -F'[][]' '{print $4}'
Reply With Quote
  #5 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Dec 2007
Posts: 21
Stumble this Post!
Hi,

Thanks guys for your help.

//racbern
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0