awk + print lines only if second field match hours:min:sec

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

King David

Posted 2019-09-25T08:18:33.360

Reputation: 405

What errors? More detail will let people help you quicker – Randomhero – 2019-09-25T09:45:34.730

see please my update – King David – 2019-09-25T09:50:53.697

Answers

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.

Kamil Maciorowski

Posted 2019-09-25T08:18:33.360

Reputation: 38 429