开发者

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 l开发者_运维知识库ine 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.


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;}' 


With GNU grep which supports Perl regular expressions:

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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜