Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex formatting using grep

Tags:

regex

grep

bash

sed

I'm parsing the IDv3 tags of mp3 files for my application and I'm using id3v2 for it. For example, to get the album name of an mp3 file the command is

id3v2 -R sample.mp3 | grep "TALB"

which outputs

TALB: Album Name

But, I want to get only the value of TALB, not the entire line so I passed it to the sed as follows

id3v2 -R sample.mp3 | grep "TALB" | sed 's/TALB: //'

which outputs

Album Name

So, here are my questions:

  1. Can the above mentioned result can be achieved only though grep. If so, how can I do it?
  2. Even though I got the result I intended to, but when I'm looking for other tags like TCON it returns example.com (255) in which I need only the example.com. So, how can I write regex in the grep for this type?
like image 916
Gowtham Avatar asked Dec 21 '25 23:12

Gowtham


1 Answers

You can use Perl-style regex with the -P flag:

grep -Po '(?<=TALB: ).*'

eg:

$ echo "TALB: Album Name" | grep -Po '(?<=TALB: ).*'
Album Name

(?<=TALB: ) is a zero-width look-behind assertion. .* is the pattern for the rest of the stuff you want to match - in the first case, everything. If you want to match only specific strings after the marker string, as in your second question, then you can change this to suit your specific data.

like image 54
Josh Jolly Avatar answered Dec 23 '25 21:12

Josh Jolly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!