Help with awk / sed

Hi i need some help with extracting text from an bash output and I am trying to learn how to use awk - sed.

I have a shell script that will mount an image file and i need to grab some script output and process it by piping the input to awk or sed.

ie.

Output :
add map loop0p1 : 0 208782 linear /dev/loop0 63
add map loop0p2 : 0 20755980 linear /dev/loop0 208845

Output :
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2

Is it possible with awk or sed to rip out :

loop0p1 and loop0p2
/dev/loop0
VolGroup00
from the text.

I know that awk should have a

match and print substr

But i dont know how to write regexps for this or how to make them work.

Any hints and tips are welcome.

Thnx

Wizkidnono

Is all the output in one file, or do you get these outputs from two different places?

A regular expression is just a piece of text, there are a few special characters which make it a lot more versatile than just wildcards but just try putting the text you want to look for between a pair of slashes. Then in the print part you probably just want one or two fields at predictable places. That's $1 for the first word of output on the matching line, $2 for the second, etc.

awk '/^add map / { print $3, $8 }'

(The ^ is one of those magical characters; it anchors this to beginning of line.)

Man that was a quick reply :slight_smile:

This will work :slight_smile:

But is there a way to rip the text out with out knowing the positions of the text ?
Allso sometimes there would be multiple instances og the field on the same line and i would like to catch them all.

Thanx

Wizkidnono

Without detailed examples it's hard to give detailed help. Yes, it can be done.

Its not easy to do mind reading over the internet :wink:

Say that my output is this:

Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2

Can i grab the VolGroup00 without knowing its exact possision or the starting or trailing text.

All i have to go by is VolGroup as the number 00 may have incremented.

I dont know if i am explaining myself properly or not but :

something like a regexp(as i know nothing about) with a constant VolGroup and a variable [0-9]

VolGroup[0-9]

Find the position and rip out the "word" VolGroup00

Thnx for helping me out.

Wizkidnono

That's perfectly understandable. In Awk Classic I guess you would simply run match() on all lines to grab the match; gawk and friends might have a way to figure out what matched in the condition.

In sed the traditional solution would be to replace everything before and after the sought regular expression with an empty string; certainly you could do that in awk too.

Seems i found a viable solution, thought i post it here.
I have now played a bit with awk and come to a solution.

Content of text.txt :

add map loop0p1 : 0 208782 linear /dev/loop0 63
add map loop0p2 : 0 20755980 linear /dev/loop0 208845
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2

Script :

awk '/VolGroup[0-9]/ {
match($0,/VolGroup[0-9][0-9]/)
print substr($0,RSTART,RLENGTH)
}' text.txt

#First we match /VolGroup[0-9]/ against file and return matching line to $0
#Then we use the match expression to match the VolGroup regexp and find char start and length
#Now we print out the substring of $0 with the start char and length.

Output :

VolGroup00

Doh.

Just found there is a PRETTY easy way to do what i wanted.
Hope this helps others to post it here.

We can just use GREP.

Command:
grep --only-matching --extended-regexp VolGroup[0-9]{2} text.txt

Output:
VolGroup01