1

log file is like this

34.3.23.58  07:20:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97
34.3.23.58  07:30:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97
34.3.23.58  07:40:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97
34.3.23.58  Average:  all                         0.02    0.00        0.02      0.00     0.00     99.96
34.3.23.60  Linux     3.10.0-514.26.2.el7.x86_64  (dns)  09/25/2019  _x86_64_  (4       CPU)

we want to capture only the lines when second field is hour:min:sec

as the following expected results

cat info.txt 

34.3.23.58  07:20:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97
34.3.23.58  07:30:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97
34.3.23.58  07:40:01  AM                          all     0.02        0.00      0.02     0.00     0.00    99.97

we try

cat info.txt | awk '$2 =~ [0-9][0-9]/:[0-9][0-9]:[0-9][0-9]'

but run with errors

awk: cmd. line:1: $2 =~ [0-9][0-9]/:[0-9][0-9]:[0-9][0-9]
awk: cmd. line:1:     ^ syntax error
awk: cmd. line:1: $2 =~ [0-9][0-9]/:[0-9][0-9]:[0-9][0-9]
awk: cmd. line:1:           ^ syntax error
awk: cmd. line:1: $2 =~ [0-9][0-9]/:[0-9][0-9]:[0-9][0-9]
awk: cmd. line:1:                ^ syntax error
awk: cmd. line:1: $2 =~ [0-9][0-9]/:[0-9][0-9]:[0-9][0-9]
awk: cmd. line:1:                  ^ unterminated regexp
  • What errors? More detail will let people help you quicker – Randomhero Sep 25 at 9:45
  • see please my update – King David Sep 25 at 9:50
2

The right syntax is

<info.txt awk '$2 ~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/'

Changes:

  • No useless use of cat.
  • The operator is ~.
  • The pattern needs to be embraced by slashes (/regex/).
  • ^ and $ match the beginning and the end of the matched string (here $2) respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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