pandronic
pandronic

Reputation: 651

How to return only part of a line with egrep

I have a program that returns something like this:

status: playing
artURL: http://beta.grooveshark.com/static/amazonart/m3510922.jpg
estimateDuration: 29400
calculatedDuration: 293000
albumName: This Is It
position: 7291.065759637188
artistName: Michael Jackson
trackNum: 13
vote: 0
albumID: 3510922
songName: Billie Jean
artistID: 39
songID: 24684170

I'm looking to extract the artist name and the song name from all this and I figured egrep would be a nice way to do it. The problem is that I have no idea how to return only part of the matching line and not the whole line.

egrep "artistName" obviously returns

artistName: Michael Jackson

I only need it to return

Michael Jackson

Any help would be appreciated. Thanks.

Upvotes: 1

Views: 5241

Answers (3)

ghostdog74
ghostdog74

Reputation: 342363

your data is structured and has a distinct field delimiter (:), so you can use awk

awk '$1~/^(artistName|songName)/{print $2}' file

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360085

With GNU grep which supports Perl regular expressions:

grep -Po '(?<=^artistName: ).*' filename

Upvotes: 3

John Kugelman
John Kugelman

Reputation: 361605

You'll need to pipe the output to another program, like cut:

egrep ^artistName | cut -d ' ' -f 2-

Or you could do the whole thing in awk or sed:

awk -F ': ' '/^artistName/ {print $2}'
sed -n '/^artistName/ {s/.*: //;p;}' 

Upvotes: 3

Related Questions