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 Extract string? namrata5 High Level Programming 2 10-24-2007 12:17 AM
Extract digits at end of string offirc Shell Programming and Scripting 6 11-20-2006 08:57 AM
Extract String sehgalniraj UNIX for Dummies Questions & Answers 1 09-25-2006 09:35 AM
Extract String bestbuyernc Shell Programming and Scripting 5 11-14-2005 12:42 PM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 07:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-20-2007
Registered User
 

Join Date: May 2007
Posts: 61
Wink Extract string between <>

hi,

I want a simple way to extract string between two angle brackets <>

the text looks like

echo "###Usage: $0 <database1> <database2>"

what I want is two variables DB1=database1 and DB2=database2

What I am doing looks clumsy to me

Code:
DB1=`echo $line | sed 's/"//g' | sed 's/>//g' | awk -F"<" '{print $2}'`
DB2=`echo $line | sed 's/"//g' | sed 's/>//g' | awk -F"<" '{print $3}'`
thanks in advance
Reply With Quote
Forum Sponsor
  #2  
Old 11-20-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
With bash/ksh93:

Code:
line='###Usage: $0 <database1> <database2>' line=(${line//[<>]})
db1="${line[2]}" db2="${line[3]}"
With zsh:

Code:
line=(${=line//[<>]})
db1="${line[3]}" db2="${line[4]}"
Or:

Code:
db1="${${(m)line%*> <*}#*<}"
db2="${${(m)line#*> <}%?}"
Reply With Quote
  #3  
Old 11-20-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
echo "${line}" | nawk -F '(<|>)' '{ printf("DB1=%s;DB2=%s\n", $2, $4)}'
Reply With Quote
  #4  
Old 11-20-2007
Registered User
 

Join Date: May 2007
Posts: 61
Thats neat

are there other ways to do it?

Thanks
Reply With Quote
  #5  
Old 11-20-2007
Moderator
 

Join Date: Feb 2007
Posts: 2,329
Code:
DB1=`echo $line | sed 's/.*<\(.*\)> <.*/\1/'`

DB2=`echo $line | sed 's/.*> <\(.*\)>"/\1/'`
Regards
Reply With Quote
  #6  
Old 11-20-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by rakeshou View Post
are there other ways to do it?

Thanks
maybe.... why?
Reply With Quote
  #7  
Old 11-20-2007
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Eek! Forget sed and awk unless you really need regexp - moderns shells are powerful enough:
Code:
#! /bin/bash

line='echo "###Usage: $0 <database1> <database2>"'

line=${line#*<}
DB1=${line%%>*}

line=${line#*<}
DB2=${line%%>*}

printf "\$DB1=$DB1 \$DB2=$DB2\n"
This works in non-ancient versions of bash and pdksh (don't have AT&T ksh handy, but I'd expect it to work there as well.)

This is faster and more elegant, in my not-so-humble opinion
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:06 AM.


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

Content Relevant URLs by vBSEO 3.2.0