1

The command mid3v2 -l someFile.mp3 gives the following output for a file with name someFile.mp3 in mp3-format:

IDv2 tag info for someFile.mp3
APIC=cover front,  (image/jpg, 52016 bytes)
TALB=someAlbumName
TCON=amusicGenre
TDRC=2000
TIT2=songname
TPE1=singer
TPE2=singer
TRCK=1

I would like to store the value of TPE1 to a variable t for further processing. How can i do this?

3

There are many ways to answer this question.
The first step is to understand that the output of a command could be received by other commands via a pipe, or, could be captured in a variable:

cmd | next command ... etc
var=$(cmd)

The process to select a line and further select what is after the sign = is called "text processing" and the shell is not well suited to do it.

A common way to do it may be sed:

 $ mid3v2 -l someFile.mp3 | sed -En 's/^TPE1=(.*)$/\1/p'
 Singer

And capture the result to a variable:

$ t=$(mid3v2 -l someFile.mp3 | sed -En 's/^TPE1=(.*)$/\1/p')
$ echo "$t"
Singer

There is no simple equivalent inside a simple shell. On higher shells (ksh,bash,zsh) it is possible to use regex. Which shell do you use?

0

Given your shell provides "process substitution", you might try "sourcing" the result:

. <(mid3v2 -l someFile.mp3 | grep -o "[^ ]*=[^ ]*")
t=$TPE1
1
  • This seem rather unsafe though. – Kusalananda Apr 12 '20 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.