I encounter error when I use gawk. Below is the my script and example file Can you guys help me? I think regex is right but there is an error when it passed to the match function. I try various approach such as give \ to special character of regex or double .
$ cat script.sh
#!/bin/bash
gawk '
BEGINFILE{
while( getline < FILENAME > 0 ){
print match($0, /[0-9]+\.[0-9]+(?= ops/s)/)
print $0
}
}
' ./file
$ cat file
123.456: IO Summary: 123456 ops 1234.567 ops/s 100/100 rd/wr 1.0mb/s 1.111ms/op
$ sh script.sh
gawk: cmd. line:4: error: Unmatched ( or \(: /[0-9]+\.[0-9]+(?= ops/
Regex in awk
or gnu-awk
don't support lookaheads. You can use this alternative gnu-awk
command:
awk 'match($0, /([0-9]+\.[0-9]+) ops\/s/, m) {print m[1]}' file
1234.567
Here is POSIX compliant awk command to do the same:
awk 'match($0, /[0-9]+\.[0-9]+ ops\/s/) {
print substr($0, RSTART, RLENGTH-6)}' file
1234.567
However if there can be multiple matches per line then use:
awk '{
s = $0
while (match(s, /([0-9]+\.[0-9]+) ops\/s/, m)) {
print m[1]
s = substr(s, RSTART + RLENGTH)
}
}' file
With your shown samples could you please try following. This should match digit with and without floating points here.
awk '
match($0,/[0-9]+(\.[0-9]+)? +ops\/s/){
val=substr($0,RSTART,RLENGTH)
sub(/ .*/,"",val)
print val
}
' Input_file
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
match($0,/[0-9]+(\.[0-9]+)? +ops\/s/){ ##using match function to match regex [0-9]+(\.[0-9]+)? +ops\/s in current line.
val=substr($0,RSTART,RLENGTH) ##Creating val variable here which has sub string of matched regex from current line.
sub(/ .*/,"",val) ##Substituting everything from space to till last with NULL in val here.
print val ##Printing val here.
}
' Input_file ##Mentioning Input_file name here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With