2

I have a string like this

TEST="/dev/sda1 on /media/sda1 type vfat (rw,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1,quiet)"

(Result from mount | grep sda1)

where I want to match the mount point. My regex (adopted from here) is

(?<=on[ \t])(.*)(?=[ \t]type)

But for some reasons it doesn't work for me with several tools (Busybox ash 1.14.2):

echo $TEST | grep -o -e '(?<=on[ \t])(.*)(?=[ \t]type)'

Gives me no result.

echo $TEST | awk 'match($0, /(?<=on )(.*)(?= type)/){print $0}'

Returns an error awk: bad regex '(?<=on[ ])(.*)(?=[ ]type)': Invalid preceding regular expression

expr match "$TEST" '\(?<=on[ \t]\)\(.*\)\(?=[ \t]type\)'

Also returns an empty result.

I feel kinda stupid.

3
  • Why don't you just try to match on (.*) type?
    – lgeorget
    Apr 22 '14 at 13:25
  • Note also that if you want the third field of a line, awk can give it to you for free: echo $TEST | awk '{print $3}'.
    – lgeorget
    Apr 22 '14 at 13:29
  • AFAIK blank is also a valid character for an USB device so this would break this code. Otherwise I could use echo $TEST | cut -d' ' -f3
    – Noir
    Apr 22 '14 at 13:34
7

Lookarounds are perl regex features. GNU grep implements them (with the -P option). I cannot say whether any busybox command does.

In this case though, you're just looking for the work after "on". Choose one of

echo "$TEST" | awk '{for (i=1; i<=NF; i++) if ($i == "on") {print $(i+1); break}}'
echo "$TEST" | sed 's/.*on[[:blank:]]\(.*\)[[:blank:]]type.*/\1/'
0

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.