'How to match space or \s via regex in awk
I am trying to match the following pattern and ended up \s not being matched. Not sure what am I missing.
This is the sample file.
There was a farmer, who had a dog
and Bingo : nameo
Bingo: nameio
and I am using following to match the string after "Bingo\s*:\s*"
awk '{for(i=1; i<=NF; i++) if($i~/Bingo[ : ]*/) print $(i+1)}' sample.log
Expecting the string "nameo" after the first match but I get ":" and second match is just fine and I get "nameio".
Can someone please help me explain why space is not being matched?
Solution 1:[1]
The [ : ]* pattern part is equal to [: ]* and represents a bracket expression that matches zero or more (as many as possible, due to *) colons or literal spaces. In your case, the spaces are used as field separators and thus are not part of the field values, so you can't use your logic with the default field separator.
There are a lot of ways to solve the problem, here are three:
awk 'match($0,/Bingo : .*/){print substr($0,RSTART+8,RLENGTH)}' file
sed -n 's/^.*Bingo : \(.*\)/\1/p' file
grep -oP 'Bingo : \K.+' file
The awk 'match($0,/Bingo : .*/){print substr($0,RSTART+8,RLENGTH)}' file command uses awk to find a record (line) with Bingo : and any text after, matching this substring, and returning a substring beginning with match start position + 8 (the length of Bingo : text).
The sed -n 's/^.*Bingo : \(.*\)/\1/p' command suppresses default line output with -n option, then finds a line with Bingo : , captures any text after this substring into Group 1, replaces the whole line with this value and only prints this result of substitution.
The GNU grep grep -oP 'Bingo : \K.+' command matches (with the PCRE regex engine, see -P option) and outputs (using -o option) one or more chars after Bingo : text (\K makes the regex engine "forget" the text matched so far).
See the online demo:
#!/bin/bash
s='There was a farmer, who had a dog
and Bingo : nameo
Bingo: nameio'
awk 'match($0,/Bingo : .*/){print substr($0,RSTART+8,RLENGTH)}' <<< "$s"
sed -n 's/^.*Bingo : \(.*\)/\1/p' <<< "$s"
grep -oP 'Bingo : \K.+' <<< "$s"
Output is nameo after each command.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
