A
A
Artyom2015-01-03 18:31:21
bash
Artyom, 2015-01-03 18:31:21

How to write a regular expression for bash?

Hello.
I understand very badly in the head. Expert help needed.
I run the command
avprobe orig/BELS.m4a
and get the following

avprobe version 9.16-6:9.16-0ubuntu0.14.04.1, Copyright (c) 2007-2014 the Libav developers
  built on Aug 10 2014 18:16:02 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2183fe0] max_analyze_duration reached
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './orig/BELS.m4a':
  Metadata:
    major_brand     : M4A 
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 2009-04-30 21:50:53
    track           : 5/8
    album           : Technical Ecstasy
    artist          : Black Sabbath
    title           : All Moving Parts (Stand Still)
    encoder         : iTunes 8.1.1.10
    date            : 1976
    gapless_playback: 1
    genre           : Музыка
  Duration: 00:05:07.38, start: 0.000000, bitrate: 966 kb/s
    Stream #0.0(und): Audio: alac, 44100 Hz, stereo, s16p, 956 kb/s
    Metadata:
      creation_time   : 2009-04-30 21:50:53
    Stream #0.1: Video: mjpeg, yuvj420p, 300x300, 90k tbn
# avprobe output

How do I write regular expressions to extract the parameters "track", "album", "artist", etc. and write them into variables?
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zr, 2015-01-03
@Zr

Regular expressions are not needed.

file="my.mp3"
declare -A MP3
while read line; do
     || continue
    IFS='=' read key value <<< "$line"
     || continue
    MP3[$key]="$value"
done <\
    <(ffprobe 2>&- -show_entries format "$file")
echo "${MP3[TAG:artist]}" 
echo "${MP3[TAG:album]}"
echo "${MP3[TAG:title]}"

Need comments?

L
ldv, 2015-01-03
@ldvldv

title=$(avprobe orig/BELS.m4a | sed -rn 's/\s+title\s+:\s+//p')
album=$(avprobe orig/BELS.m4a | sed -rn 's/\s+album\s+:\s+//p')
artist=$(avprobe orig/BELS.m4a | sed -rn 's/\s+artist\s+:\s+//p')

3
3vi1_0n3, 2015-01-04
@3vi1_0n3

You can do this, almost in one line:

#!/bin/bash
. <(avprobe orig/BELS.m4a | grep "title\|album\|artist" | sed -r 's/\s*([^ ]*)\s*:\s*(.*)$/\1=\"\2\"/')
echo $album
echo $title
echo $artist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question