You are not logged in.

#1 2008-04-08 21:28:05

wyvern
Member
From: 'Old' Scottish/English border.
Registered: 2008-01-06
Posts: 70

Awk - printing a value between two matching regex [SOLVED]

I'm trying to write a script to parse a single line output, but to then only display the value between two matching regex.

I have got it to only display the value between the matching regex, but then it carries on and displays the rest of the line. How do I tell it to stop once the matching value has been printed?

ARTIST=`awk '/Artist/,/Artist/ {gsub(/Artist/, Artist); print}' /home/sapphire/.foobar2000/track_info`

This produces 'Akira Yamaoka Title She Title Album Silent Hill 1 OST Album CurrentTime 0:03 CurrentTime TotalTime 2:01 TotalTime' rather then the desired 'Akira Yamaoka'.

So I guess I'm asking how one would go about terminating the above awk command? Thanks - I know, it'll be something simple - but I'm not experienced with scripting roll

Last edited by wyvern (2008-04-10 11:37:30)

Offline

#2 2008-04-09 10:41:31

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: Awk - printing a value between two matching regex [SOLVED]

Could you share the file you are extracting the data from? (track_info)
Maybe the ideal solution has a different approach then yours.

Offline

#3 2008-04-09 15:28:39

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: Awk - printing a value between two matching regex [SOLVED]

you can use the RS (record separator) and the FS (field separator)

awk 'BEGIN{ RS="Artist "; FS="Title "} {print $1}' /foo/bar/foobar.foo

If I understood your question right.

Offline

#4 2008-04-09 18:17:40

wyvern
Member
From: 'Old' Scottish/English border.
Registered: 2008-01-06
Posts: 70

Re: Awk - printing a value between two matching regex [SOLVED]

ibendiben wrote:

Could you share the file you are extracting the data from?

No problem, and thank you for the help so far smile

This is what's written each time I update my playing track, no more, no less:

Artist Akira Yamaoka Artist Title Silent Hill Title Album Silent Hill 1 OST Album CurrentTime 0:04 CurrentTime  TotalTime 2:51 TotalTime

Single line only, as the plugin won't break up the text into anything more than the one line sad

Offline

#5 2008-04-09 18:32:51

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: Awk - printing a value between two matching regex [SOLVED]

I think there is no need to mangle with RS here; FS itself should be enough, something like

awk -F 'Artist' '{print $2}'

will extract Akira Yamaoka as desired. As FS is a regex, one can use

-F 'Artist|Title|Album|&so on'

to split the record into individual fields.

Offline

#6 2008-04-09 18:42:03

wyvern
Member
From: 'Old' Scottish/English border.
Registered: 2008-01-06
Posts: 70

Re: Awk - printing a value between two matching regex [SOLVED]

briest wrote:

As FS is a regex, one can use -F 'Artist|Title|Album|&so on'
to split the record into individual fields.

Oh, that's perfect! Thank you. Far simpler too - I was being overly-complex with gsub instead of FS - I hadn't realised I could do that roll

Anyways, thank you all big_smile

Offline

#7 2008-04-09 20:06:21

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: Awk - printing a value between two matching regex [SOLVED]

I like to use sed in stead:

sed 's#Artist \(.*\) Artist Title .*$#\1#' /yourfile

Doesn't matter much, but once the files get bigger, it is faster.

Offline

Board footer

Powered by FluxBB