You are not logged in.

#1 2015-01-26 05:06:41

kdelwat
Member
Registered: 2015-01-26
Posts: 1

Help needed extracting information from command output using sed

I want to be able to pipe the output of:

playerctl metadata

into sed and extract only the relevant information. I have a working knowledge of regular expressions but I'm not how to get sed to only output the matched text. For reference, this is an example output of the playerctl command:

{'mpris:artUrl': <'http://open.spotify.com/thumb/ddf652a13affe3346b8f65b3a92bd3abfdbf7eca'>, 'mpris:length': <uint64 290000000>, 'mpris:trackid': <'spotify:track:0xqi1uFFyefvTWy0AEQDJ7'>, 'xesam:album': <"Smokey's Haunt">, 'xesam:artist': <['Urthboy']>, 'xesam:autoRating': <0.23000000000000001>, 'xesam:contentCreated': <'2012-01-01T00:00:00'>, 'xesam:discNumber': <1>, 'xesam:title': <'On Your Shoulders'>, 'xesam:trackNumber': <6>, 'xesam:url': <'spotify:track:0xqi1uFFyefvTWy0AEQDJ7'>}%      

I'm trying to get the track name and artist name and combine them into one string. Perhaps sed isn't the tool for the job but I'm not really sure what else to try. Any help is very appreciated.

Offline

#2 2015-01-26 06:17:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Help needed extracting information from command output using sed

If that was valid json, which it doesn't appear to be, you could use jshon to query it.

Are you sure that is the exact output of the command? It looks sort of off...

In the meatime, this is pretty hacky:

awk 'BEGIN { RS=","; FS="'\''" }; /artist/ { artist = $4 }; /title/ { title = $4 } END { print artist": "title }' file
Urthboy: On Your Shoulders

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB