Skip to main content
added 10 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP '(?=^|;<=^|;)creq_recv=[^;]+'

Or, safer in case we can have other field names that end with creq_recv:

tail -f stats_2022102116.log | grep -oP '(^|;)\Kcreq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | 
    awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i } } }'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP '(?=^|;)creq_recv=[^;]+'

Or, safer in case we can have other field names that end with creq_recv:

tail -f stats_2022102116.log | grep -oP '(^|;)\Kcreq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i}}}'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP '(?<=^|;)creq_recv=[^;]+'

Or, safer in case we can have other field names that end with creq_recv:

tail -f stats_2022102116.log | grep -oP '(^|;)\Kcreq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | 
    awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i } } }'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

added 163 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP 'creq_recv=[^;]+''(?=^|;)creq_recv=[^;]+'

Or, safer in case we can have other field names that end with creq_recv:

tail -f stats_2022102116.log | grep -oP '(^|;)\Kcreq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i}}}'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP 'creq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i}}}'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP '(?=^|;)creq_recv=[^;]+'

Or, safer in case we can have other field names that end with creq_recv:

tail -f stats_2022102116.log | grep -oP '(^|;)\Kcreq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i}}}'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.

Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

You can do what you want just with grep with the -o option to only print the matching portion of the line and -P or -E for PCRE or extended regular expressions respectively, which let us use [^;]+ for "one or more non-; characters":

tail -f stats_2022102116.log | grep -oP 'creq_recv=[^;]+'

Your awk was failing because -v creq_recv="$1" means "set the value of the awk variable creq_recv to the value of the first argument passed to the script". But since you are launching the script with no arguments (./chk_conn_log.sh), $1 is empty, so creq_recv is also empty.

In any case, that wouldn't have worked, even if the variable were being set. Both because the awk script was wrong and because tail -f will never exit unless you stop it, so the echo wouldn't be executed. You wanted something like this:

#!/bin/bash

tail -f stats_2022102116.log | awk -F';'  '{ for(i=1; i<=NF; i++){ if ($i ~ /^creq_recv=/) { print $i}}}'

No echo, nothing but the tail and the corrected awk script. But just use the grep command above, it's much simpler.