Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bash Multi-Line Regular Expression

1,831 views
Skip to first unread message

Alan Gutierrez

unread,
Jun 25, 2011, 1:12:10 PM6/25/11
to
How do I match the beginning of a line in multi-line text using Bash
regular expressions?

[[ $(/bin/mount) =~ /dev ]] && echo YES

That works, but this doesn't.

[[ $(/bin/mount) =~ ^/dev ]] && echo YES

Nor this:

[[ $(/bin/mount) =~ ^/dev ]] && echo YES

But this does:

[[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES

--
Alan Gutierrez - http://twitter.com/bigeasy - http://github.com/bigeasy

mhenn

unread,
Jun 25, 2011, 3:46:22 PM6/25/11
to
Am 25.06.2011 19:12, schrieb Alan Gutierrez:
> How do I match the beginning of a line in multi-line text using Bash
> regular expressions?
>
> [[ $(/bin/mount) =~ /dev ]] && echo YES
>
> That works, but this doesn't.
>
> [[ $(/bin/mount) =~ ^/dev ]] && echo YES
for me this works:

bash-4.1$ bash --version
GNU bash, Version 4.1.5(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
Lizenz GPLv3+: GNU GPL Version 3 oder jᅵnger
<http://gnu.org/licenses/gpl.html>

Dies ist freie Software. Sie darf verᅵndert und verteilt werden.
Fᅵr dieses Programm besteht keinerlei Garantie.
bash-4.1$ [[ $(/bin/mount) =~ ^/dev ]] && echo YES
YES
bash-4.1$

>
> Nor this:
>
> [[ $(/bin/mount) =~ ^/dev ]] && echo YES

Where's the difference to the one before?


>
> But this does:
>
> [[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES
>

works as well for me (but this matches by the way another string)

bash-4.1$ [[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES
YES
bash-4.1$

Alternatively you could use (yes, one more process...):
/bin/mount | egrep -q '^/dev' && echo Yes

--
Michael


DennisW

unread,
Jun 25, 2011, 8:42:27 PM6/25/11
to

What version of Bash?

Does your version of mount output a whitespace character before "/
dev"?

Does this work for you?:

[[ $(/bin/mount) =~ ^[[:space:]]*/dev ]] && echo YES


This, using globs instead of regexes, should also work:

[[ $(/bin/mount) = /dev* ]] && echo YES

or some variation.

Alan Gutierrez

unread,
Jun 26, 2011, 8:02:56 AM6/26/11
to
On 6/25/11 3:46 PM, mhenn wrote:
> Am 25.06.2011 19:12, schrieb Alan Gutierrez:
>> How do I match the beginning of a line in multi-line text using Bash
>> regular expressions?
>>
>> [[ $(/bin/mount) =~ /dev ]]&& echo YES
>>
>> That works, but this doesn't.
>>
>> [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
>>
>> Nor this:
>>
>> [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
>
> Where's the difference to the one before?

I was cutting and pasting too fast. I meant to say:

Nor this:

[[ $(/bin/mount) =~ \n/dev ]]&& echo YES

>> But this does:
>>
>> [[ $(/bin/mount) =~ [[:space:]]/dev ]]&& echo YES
>>
> works as well for me (but this matches by the way another string)
>
> bash-4.1$ [[ $(/bin/mount) =~ [[:space:]]/dev ]]&& echo YES
> YES
> bash-4.1$

> Alternatively you could use (yes, one more process...):


> /bin/mount | egrep -q '^/dev'&& echo Yes

Yes. That's what I did for now. Trying to learn the ins and outs of the
built-in regular expressions.

--
Alan Gutierrez

Alan Gutierrez

unread,
Jun 26, 2011, 8:04:48 AM6/26/11
to
On 6/25/11 3:46 PM, mhenn wrote:
> Am 25.06.2011 19:12, schrieb Alan Gutierrez:
>> How do I match the beginning of a line in multi-line text using Bash
>> regular expressions?
>>
>> [[ $(/bin/mount) =~ /dev ]]&& echo YES
>>
>> That works, but this doesn't.
>>
>> [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
> for me this works:
>
> bash-4.1$ bash --version
> GNU bash, Version 4.1.5(1)-release (i686-pc-linux-gnu)
> Copyright (C) 2009 Free Software Foundation, Inc.
> Lizenz GPLv3+: GNU GPL Version 3 oder jünger
> <http://gnu.org/licenses/gpl.html>
>
> Dies ist freie Software. Sie darf verändert und verteilt werden.
> Für dieses Programm besteht keinerlei Garantie.

> bash-4.1$ [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
> YES
> bash-4.1$

You probably have a /dev at the very start of your mount output. I
don't. I'm trying to have ^ match the start of line, or else explicitly
match a newline or start of line. This would be fine.

[[ $(/bin/mount) =~ ^/dev|\n/dev ]] && echo YES

mhenn

unread,
Jun 26, 2011, 9:02:28 AM6/26/11
to
Am 26.06.2011 14:04, schrieb Alan Gutierrez:
> On 6/25/11 3:46 PM, mhenn wrote:
>> Am 25.06.2011 19:12, schrieb Alan Gutierrez:
>>> How do I match the beginning of a line in multi-line text using Bash
>>> regular expressions?
>>>
>>> [[ $(/bin/mount) =~ /dev ]]&& echo YES
>>>
>>> That works, but this doesn't.
>>>
>>> [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
>> for me this works:
>>
>>[...]

>> bash-4.1$ [[ $(/bin/mount) =~ ^/dev ]]&& echo YES
>> YES
>> bash-4.1$
>
> You probably have a /dev at the very start of your mount output. I
> don't. I'm trying to have ^ match the start of line, or else explicitly
> match a newline or start of line. This would be fine.
>
> [[ $(/bin/mount) =~ ^/dev|\n/dev ]] && echo YES
>
That's right, i have a "/dev" at the very start of my mount output.
Trying to do, what you want here is getting us some difficulties i think :)
The thing is, that bash does /not/ recognize the "\n"! So you have to
put a newline into your pattern somehow. Trying this:

[[ $(echo -e "foo\nbar") =~ "^bar|
bar" ]] && echo Yes

i found out that it does not work (I'm not sure myself, why this does
not work.) What however works is the following idea:

[[ $(echo -e "foo\nbar") =~ $(echo -e "^bar|\nbar") ]] && echo Yes

And echo -e "foo\nfoobar" correctly does not match. So this is one
solution for you using only shell builtins.

At the moment i can't think of another way to get this newline into your
pattern.

Have a nice day,
Michael

0 new messages