简体   繁体   中英

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.

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

使用支持Perl正则表达式的GNU grep

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

您的数据是结构化的,并且具有不同的字段定界符(:),因此您可以使用awk

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM