Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed question 1

Status
Not open for further replies.

zaxxon

MIS
Dec 12, 2001
226
DE
Hi all,

here the string to parse:
Code:
<Device dev_name="0853" status="Ready" configuration="RAID-5">

I only need 0853.

I tried:

Code:
sed 's/.*dev_name=\"\(.*[^\"]\)\" .*/\1/g'

But I always get:

Code:
0853" status="Ready

I used definitions like [0-9a-zA-Z]\{4\} to be more precise (and which works) but I was told, that it's not always sure, that it will be 4 characters and not sure if it's always from that pool of characters.

Thanks in forward.

laters
zaxxon
 
Code:
sed 's/.*dev_name=\"\([^\"]*\)\" .*/\1/'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or may be
Code:
sed 's/.*dev_name=\"\(.+\)?\" .*/\1/'


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Or another idea (assuming the position of the required field is always the same):

[tt]awk -F '[<=" ]+' '{print $4}'[/tt]

Annihilannic.
 
PHV,

can you explain to me about ur code around this:
([^\"]*\)

I know it's working but if I translate it should be anything match with ^or" then capture the rest or just anything start with " ?

Because I tried this :
Code:
echo '<Device dev_name=$0853" status="Ready" configuration="RAID-5">' | sed 's/.*dev_name=\$\([^"]*\)\".*/\1/'

it's still working even I replace the " with $

thanks man,
 
This is not PHV but I just arrived at work and read it, so...

[^\"]* stands stands for:

A character of any type but it may not be a quotation mark and so many "characters being not a quotation mark" as you like.
The slash between ^ and " is just to escape the ".
A ^ in those [] brackets means "not" while outside it stands for the beginning of a line.

The \( and \) around it are just the brackets to group it so you can give it out with \1.



laters
zaxxon
 
Ooo ok..

I didn't know that ^ can mean "not"
I thought in regex it means "start with", is this normal regex or special sed command?

Thanks zaxxon,
 
RegEx can be different. It depends on which "language" or tool you use. Perl RegEx might not be the same as sed for example. There are usually some differences between some of them afaik.
I usually only use sed, awk and grep/egrep and those are usually the same or lets say compatible.

I remember a book from O'Reilly about Regular Expressions which has different chapters, one for Perl, one for... and so on.

laters
zaxxon
 
<Device dev_name="0853" status="Ready" configuration="RAID-5">

I would use the following:

grep Device |
awk '{print $1}' |
sed -e 's/"//g' |
cut -d= -f2

This way it's very clean and every step is very straightforward.

 
zaxxon,

A character of any type but it may not be a quotation mark and so many "characters being not a quotation mark" as you like.

I replaced
[^\"]*
becomes
[^3]* or [^\3]*

which I'm expecting 085, but it doesn't work.According to your explanation it should work.
 
It didn't work because sed did not found a number you specified, which had no 3 in it and ended with a ".
Make it
Code:
[^3]*\)3\"
so it will work again, as I described. Since you said the group of characters may not have a 3 in it, you have to tell it, that the 3 shall be the new ending character. Formerly it was only the escaped " showing sed the end of the wanted characters inside the () or escaped \(\).


laters
zaxxon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top