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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2007
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
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
  #2 (permalink)  
Old 11-20-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
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#*> <}%?}"
  #3 (permalink)  
Old 11-20-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

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

Join Date: May 2007
Posts: 75
Thats neat

are there other ways to do it?

Thanks
  #5 (permalink)  
Old 11-20-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

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

DB2=`echo $line | sed 's/.*> <\(.*\)>"/\1/'`
Regards
  #6 (permalink)  
Old 11-20-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

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

Thanks
maybe.... why?
  #7 (permalink)  
Old 11-20-2007
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
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
Sponsored Links
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 03:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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