All,
I have file names that will be always in the following format "rX_Q_Y_filename.mp3" where X and Y are numbers (0-9). FYI, X and Y in the filename can be 1 or more digits
example "r1234_Q_456789_filename.mp3"
How do I extract the 'Q_456789" part of the file name using bash script?
4 Answers 4
For bash:
# See "modern regexps" in regex(7) for regexp syntax.
# The intermediate $re variable is not necessary, but it removes the
# need for having to escape every single metacharacter in the regexp.
re='_(Q_[[:digit:]]+)'
if [[ $name =~ $re ]]; then
part=${BASH_REMATCH[1]}
echo "Matched on $part"
else
echo "Match failed"
fi
I am not sure if this is exactly what you are after, but:
ls -1 r*.mp3 | egrep -o Q_[0-9]+
will return a list of Q_{numbers}
$ ls r*.mp3
r1234_Q_456789_test.mp3
$ ls -1 r*.mp3 | egrep -o Q_[0-9]+
Q_456789
Here's an inefficient way of doing it with awk.
$ ls -1 *.mp3
r1234_Q_433_filename.mp3
r1234_Q_456789_filename.mp3
r323_Q_433_filename.mp3
$ ls -1 *.mp3 | awk '{print substr(1,ドル match(0,ドル /Q/), (match(0,ドル /\_f/) - match(0,ドル /Q/)))}'
Q_433
Q_456789
Q_433
Sed oneliner:
sed 's/[^Q]*\(Q_[0-9]*\)_.*/1円/' <<<"r1234_Q_456789_filename.mp3"
Q_456789
'Q' is the first character your interested in so throw away everything before it, group the 'Q_number' and then trash the rest, substitute the back-reference.
$your_string =~ /r.+_(Q_\d+)_filename\.mp3/; print 1ドル;