Extract string between two characters in bash

I have a string formatted below

Walk Off the Earth - Somebody That I Used to Know
[playing] #36/37   1:04/4:05 (26%)
volume: n/a   repeat: off   random: on    single: off   consume: off

Now from this line I need to extract 36from #36/37.

The first thing I did was extract #36/37from the second line using

echo "above mentioned string" | awk 'NR==2 {print $2}'

Now I want to extract 36from the above part for which I did

echo `#36/37` | sed -e 's/\//#/g' | awk -F "#" '{print $2}'

who gave me 36as my outptut.

But I feel that using sed and awkonly to extract text from #36/37is just superfluous. So, is there a better or shorter way to achieve this.

+5
source share
8 answers

Divide the field by the pound and slash characters in the array and extract the required element.

awk 'NR==2 {split($2, arr, "[#/]"); print arr[2]}'
+3
source

bash =~. ( test, , test. [[.)

mini:~ michael$ cat foo
Walk Off the Earth - Somebody That I Used to Know
[playing] #36/37   1:04/4:05 (26%)
volume: n/a   repeat: off   random: on    single: off   consume: off

mini:~ michael$ [[ $(<foo) =~ \#[[:digit:]]{2} ]] && echo "${BASH_REMATCH[0]#\#}"
36

, , , BASH_REMATCH.

+3

sed, infile, . #, 1 \1. -n , p .

sed -ne '2 { s/^[^#]*#\([0-9]*\).*$/\1/; p; q }' infile

:

36
+2

:

sed 's/.*#\([0-9]*\)\/[0-9]*.*/\1/p;d' file
36
+2
input | while read playing numbers rest
do
  if [[ $playing = "[playing]" ]]; then
    t="${numbers:1}"
    echo "${t%/*}"
  fi
done

Bash - , () . - bash : , "/"

+2
sed -n '2s/.*\#\([0-9]*\)\/.*/\1/p'

, , # /

+2

.

awk -F'[#/]' 'NR==2{print $2}'
+1

script, . , script.

echo '[playing] #36/37   1:044:05 (26%)' | cut -d' ' -f2 | ./cut_between.sh -f '#' -l '/'

script GitHub.

0

All Articles