![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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}'`
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
With bash/ksh93:
Code:
line='###Usage: $0 <database1> <database2>' line=(${line//[<>]})
db1="${line[2]}" db2="${line[3]}"
Code:
line=(${=line//[<>]})
db1="${line[3]}" db2="${line[4]}"
Code:
db1="${${(m)line%*> <*}#*<}"
db2="${${(m)line#*> <}%?}"
|
|
#3
|
||||
|
||||
|
Code:
echo "${line}" | nawk -F '(<|>)' '{ printf("DB1=%s;DB2=%s\n", $2, $4)}'
|
|
#4
|
|||
|
|||
|
Thats neat
are there other ways to do it?
Thanks |
|
#5
|
|||
|
|||
|
Code:
DB1=`echo $line | sed 's/.*<\(.*\)> <.*/\1/'` DB2=`echo $line | sed 's/.*> <\(.*\)>"/\1/'` |
|
#6
|
||||
|
||||
|
#7
|
||||
|
||||
|
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 is faster and more elegant, in my not-so-humble opinion |
||||
| Google The UNIX and Linux Forums |