问题:

问题:

所以基本上,我试图通过编程从 mp3 中获取评级,并使用命令行工具 id3v2,我可以获得我的程序对其赋予的评级:

$ id3v2 -R Drake\ -\ Over3.mp3
id3v1 tag info for Drake - Over3.mp3:
Title  : Over                            Artist: Drake
Album  : Thank Me Later                  Year: 2010, Genre: Unknown (255)
Comment: The highly anticipated debut    Track: 0
id3v2 tag info for Drake - Over3.mp3:
TPE2 (Band/orchestra/accompaniment): Drake
TIT2 (Title/songname/content description): Over
TPE1 (Lead performer(s)/Soloist(s)): Drake
TALB (Album/Movie/Show title): Thank Me Later
TYER (Year): 2010
TCON (Content type): Rap - Hip-Hop (255)
TPUB (Publisher): Cash money/Universal Motown
POPM (Popularimeter): Windows Media Player 9 Series, counter=0 rating=196COMM (Comments): (MusicMatch_Preference)[eng]: Very Good
COMM (Comments): ()[eng]: The highly anticipated debut from Drake is here! "Thank Me Later" is hotest album in the game.
APIC (Attached picture): ()[, 3]: image/jpg, 38227 bytes
COMM (Comments): (ID3v1 Comment)[XXX]: The highly anticipated debut
TRCK (Track number/Position in set): PUB

我可以缩小到

$ id3v2 -R Drake\ -\ Over3.mp3 | grep POPM
POPM (Popularimeter): Windows Media Player 9 Series, counter=0 rating=196COMM (Comments): (MusicMatch_Preference)[eng]: Very Good

问题:

我不确定如何从这个字符串中获取“rating=###”。我的 awk/sed-fu 很弱 :(

答案1

grep -o 'rating=[[:digit:]]\+'

对我有用......

答案2

您可以grep通过使用一次调用来避免使用两次调用sed

id3v2 -R Drake\ -\ Over3.mp3 | sed -n '/POPM/s/.*[[:blank:]]\(rating=\)\([[:digit:]]\+\)\([^[:blank:]]*\)[[:blank:]].*/\1\2\3/p'

您可以通过删除反向引用来选择要输出的内容。在您的示例中,反向引用输出以下内容:

  • \1- 评分=
  • \2- 196
  • \3- 通讯

合起来:“评级=196COMM”

相关内容