lebossejames
Posts: 32
Joined: Thu Jun 20, 2019 2:10 pm

awk regular expression

Fri Sep 17, 2021 6:38 pm

Hello,

I try my bash script on my raspbian buster, i get this error of this command (goal get path of my external devices):

Code: Select all

lsblk | grep sd | awk '{if ( $7~/[/][_a-zA-Z0-9-]*[/][_a-zA-Z0-9-]*[/]*$ ) print $7;}'
awk: line 1: regular expression compile failed (bad class -- [], [^] or [)
[
awk: line 1: syntax error at or near ]
awk: line 1: regular expression compile failed (bad class -- [], [^] or [)
][_a-zA-Z0-9-]*[


Works fine on my computer with Ubuntu.

Have you an idea of my problem?

Thank you.

trejan
Posts: 3735
Joined: Tue Jul 02, 2019 2:28 pm

Re: awk regular expression

Fri Sep 17, 2021 7:15 pm

lebossejames wrote:
Fri Sep 17, 2021 6:38 pm
Works fine on my computer with Ubuntu.
You're missing a / after the $
lebossejames wrote:
Fri Sep 17, 2021 6:38 pm
Have you an idea of my problem?
Debian uses mawk. Ubuntu uses gawk. They're not the same. Use \/ instead of [/] as mawk considers / to be the start of an ERE constant.

jbudd
Posts: 1911
Joined: Mon Dec 16, 2013 10:23 am

Re: awk regular expression

Sat Sep 18, 2021 2:49 am

Or install and use gawk. There will still be a / missing from the end of your pattern though, as Trejan says.

lebossejames
Posts: 32
Joined: Thu Jun 20, 2019 2:10 pm

Re: awk regular expression

Sat Sep 18, 2021 7:47 am

You say :

Code: Select all

lsblk | grep sd | awk '{if ( $7 ~ /\[_a-zA-Z0-9-]*/\[_a-zA-Z0-9-]*/\*$/ ) print $7;}'
awk: 1: unexpected character '\'


?

User avatar
jahboater
Posts: 7399
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: awk regular expression

Sat Sep 18, 2021 7:55 am

lebossejames wrote:
Sat Sep 18, 2021 7:47 am
You say :

Code: Select all

lsblk | grep sd | awk '{if ( $7 ~ /\[_a-zA-Z0-9-]*/\[_a-zA-Z0-9-]*/\*$/ ) print $7;}'
awk: 1: unexpected character '\'


?
As trejan said above, use \/ not /\
You are escaping the delimiter.

ame
Posts: 3743
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: awk regular expression

Sat Sep 18, 2021 10:43 am

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Jamie Zawinski

User avatar
jahboater
Posts: 7399
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: awk regular expression

Sat Sep 18, 2021 11:24 am

It could be simplified or made more readable.

For example, inside the [] character classes, use [:alnum:] instead of a-zA-Z0-9
Does awk allow delimiters other than / ??

lebossejames
Posts: 32
Joined: Thu Jun 20, 2019 2:10 pm

Re: awk regular expression

Sat Sep 18, 2021 12:44 pm

jahboater wrote:
Sat Sep 18, 2021 7:55 am
lebossejames wrote:
Sat Sep 18, 2021 7:47 am
You say :

Code: Select all

lsblk | grep sd | awk '{if ( $7 ~ /\[_a-zA-Z0-9-]*/\[_a-zA-Z0-9-]*/\*$/ ) print $7;}'
awk: 1: unexpected character '\'


?
As trejan said above, use \/ not /\
You are escaping the delimiter.

I get now:

Code: Select all

lsblk | grep sd | awk 'if ( $7 ~ \/[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ ) {print $7;}'
awk: ligne de commande:1: if ( $7 ~ \/[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ ) {print $7;}
awk: ligne de commande:1: ^ syntax error
awk: ligne de commande:1: if ( $7 ~ \/[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ ) {print $7;}
awk: ligne de commande:1:           ^ la barre oblique inverse n'est pas le dernier caractère de la ligne
awk: ligne de commande:1: if ( $7 ~ \/[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ ) {print $7;}
awk: ligne de commande:1:           ^ syntax error


GlowInTheDark
Posts: 2041
Joined: Sat Nov 09, 2019 12:14 pm

Re: awk regular expression

Sat Sep 18, 2021 1:09 pm

It could be simplified or made more readable.
No doubt, but I didn't want to hijack this thread into another "human compiler" thing.
For example, inside the [] character classes, use [:alnum:] instead of a-zA-Z0-9
FWIW, as a matter of style, I never quite cottoned to those "character class" thingies; I avoid using them. It seems weird things happen when you do - you start to begin to worry about "Well, with whatever LANG (and so on) settings I have, is 7 still considered to be a digit?" type questions.
Does awk allow delimiters other than / ??
No. And it can be annoying at times, but you learn to live with it. Yes, I know that "sed" and "vi" (i.e., "ex") do allow it.
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

User avatar
jahboater
Posts: 7399
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: awk regular expression

Sat Sep 18, 2021 1:23 pm

GlowInTheDark wrote:
Sat Sep 18, 2021 1:09 pm
FWIW, as a matter of style, I never quite cottoned to those "character class" thingies; I avoid using them. It seems weird things happen when you do - you start to begin to worry about "Well, with whatever LANG (and so on) settings I have, is 7 still considered to be a digit?" type questions.
Isn't that one of the benefits? For example [A-Z] works fine in any sensible character set, but I don't think it will work with EBCDIC which has gaps (common on mainframes). Whereas \a or [:alpha:] probably would work.

Its true CCL's can get very complicated unless you are comfortable with set theory.
Some applications allow subtraction .....
[[:alnum:]-[:digit:]] is the same as [:alpha:]

jbudd
Posts: 1911
Joined: Mon Dec 16, 2013 10:23 am

Re: awk regular expression

Sat Sep 18, 2021 1:36 pm

Edited!

Awk is a pattern / action language.
A pattern is contained between / and /
An action is contained between { and }
When data matches the pattern, the action is performed.
And on the command line you have to enclose the whole awk program between single quotes.

You are using if( ) which (I think) has to be within the action. But you don't need if( ). Awk is designed specifically to do stuff if a field or fields match the pattern.

All of these should work:
lsblk | grep sd | awk '{ if( $7 ~ /[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/) print $7}'
lsblk | grep sd | awk '$7 ~ /[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ {print $7}'
lsblk | grep sd | awk '/[_a-zA-Z0-9-]*\/[_a-zA-Z0-9-]*\/*$/ {print $7}'
lsblk | awk '/sd[a-z][0-9]/ {print $7}'

And maybe even these, though you get a blank line.
lsblk | grep sd | awk '{print $7}'
lsblk | awk '/sd/ {print $7}'
Attachments
Untitled 5.jpg
Untitled 5.jpg (105.63 KiB) Viewed 473 times
Last edited by jbudd on Sat Sep 18, 2021 2:12 pm, edited 7 times in total.

GlowInTheDark
Posts: 2041
Joined: Sat Nov 09, 2019 12:14 pm

Re: awk regular expression

Sat Sep 18, 2021 1:52 pm

Isn't that one of the benefits?
Well, that's the thing. A "2 kinds of people in the world" sort of thing. Suppose the politicians declare that 7 is no longer a digit (don't laugh, it could happen). There are going to be two kinds of reactions to this news:

1) People who want to go along with it. Who want to be kept appraised of the latest changes.

2) People who want their code to continue to work predictably, ignoring the whims of the pols.

I'm in group 2.
(Even though most people are in group 1)
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

lebossejames
Posts: 32
Joined: Thu Jun 20, 2019 2:10 pm

Re: awk regular expression

Sat Sep 18, 2021 8:34 pm

Thank you, it's works fine now.

Return to “Beginners”