3

I have the following line:

    given some books I've given to my son.

Notice the four spaces in front of the "Given". I want to match the "given" following whitespace at the beginning of the line with a regex. I do not want the second "given" to match.

If I use \s*given it will match both words. If I add the ^ for the beginning of the line (^\s*given) it does not match either.

Try to enter \s*The and ^\s*The on this RegexOne example to understand the problem.

Edit

For some reason, the fox example works now and the regex works on another site, so here's my full example:

  given an egg
    and some milk
    and the ingredient flour
   when the cook mangles everything to a dough
    and the cook fries the dough in a pan
   then the resulting meal is a pan cake

And my awk expressions that all don't match:

/^\s*given/ { print "given()."}
/^[\s]*and/ { print "and()."}
/^\s*when/ { print "when()."}
/^\s*then/ { print "then()."}

They all match once I remove the ^.

9
  • Please check this demo. Isn't it what you need? Jul 25, 2015 at 16:19
  • It is. Why is it working in that demo and not in awk and on the RegexOne site? :-(
    – Dominik
    Jul 25, 2015 at 16:21
  • If you type ^\s*The on RegexOne, you will get the first The highlighted. It works there. It is actually correct and is what you need. Jul 25, 2015 at 16:30
  • Yes. This leaves me even more confused. I've updated the question with the real life example.
    – Dominik
    Jul 25, 2015 at 16:34
  • grep -oP '^\s*\K\w*', awk '{print $1}', sed 's/\s*\(\S*\).*/\1/' … everyone is work. Try to pass lines through hd -c to see if there are \s on the begining
    – Costas
    Jul 25, 2015 at 16:50

2 Answers 2

2

As Ed Morton mentioned, some Awks (such as The One True Awk) only support POSIX character classes, so \s is not matching whitespace, it's matching the letter s.

Since you're using * to match zero or more occurrences:

awk '/\s*given/' file

matches because there are zero occurrences of s at the beginning of the line, whereas:

awk '/^\s*given/' file

will never match because there are unmatched characters (whitespace) between ^ (start of line) and the word given.

If you were to use + to match one or more occurrences, you'd see that this does not work either:

awk '/\s+given/' file

so the obvious solution is to use [[:space:]]:

awk '/^[[:space:]]*given/' file

But since Awk's default is to split fields by whitespace, if you wish to match a word against the first set of non-whitespace characters, it's more straight forward to compare the word with the first field $1.

awk '$1 == "given"' file

to match completely, or:

awk '$1 ~ /^given/' file

to match against the beginning of the first field.

As an aside, if you want to test your regex against a set of words and print them appended with ()., as is shown in your example, you could use the string functions match and substr like this:

awk '{
    m = match($0, /^[[:space:]]*(given|and|when|then)/) # or match($1, /.../)
    if(m)
        print substr($1, RSTART, RSTART+RLENGTH) "()."
}' file

output:

given.()
and.()
and.()
when.()
and.()
then.()
1

This regex can match what you're looking for:

^[[:space:]]*given

It matches all the whitespace characters in the front including the first "given".

You can play with it here:

https://regex101.com/r/yA5dV0/1


Edit: Changed it to Ed Morton's suggestion.

1
  • 3
    A "regexp" is close to useless without which tool handles that regexp as desired. Most awks, for example, would treat that regexp as a start-of-string, then a letter s repeated 0 or more times, and then the word given and some of them would warn you about the useless escape char before the s, That's probably not what the OP wants.
    – Ed Morton
    Jul 26, 2015 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.