0

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?

asked Oct 7, 2011 at 5:49
1
  • Why not use Perl? It's way easier: $your_string =~ /r.+_(Q_\d+)_filename\.mp3/; print 1ドル; Commented Oct 7, 2011 at 8:18

4 Answers 4

2

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
answered Oct 7, 2011 at 6:03
2

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
answered Oct 7, 2011 at 5:59
0

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
answered Oct 7, 2011 at 6:27
0

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.

answered Oct 24, 2011 at 18:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.