2

I am using sed to capture and print only specific timestamps which are before 12pm (i.e. having 0 as first digit eg 09 as hours)

sed  -E 's/^.*\[(.* 0.*)\].*/\1/g'

However, it's also printing lines where no group is captured. How to avoid printing those?

Following is output of above command. Where regex matched, only captured group is printed and where it didn't match, original string is printed. I want to avoid printing string if regex is not matched.

2021/03/01 09:11:14
Accessed ([2021/03/01 12:32:36])
Accessed ([2021/03/01 16:48:29])
Accessed ([2021/03/01 19:40:03])
2021/03/02 08:53:27
Accessed ([2021/03/02 11:03:23])
Accessed ([2021/03/02 11:04:08])
Accessed ([2021/03/02 16:36:48])
Accessed ([2021/03/02 19:35:31])
2021/03/03 08:38:10
anubhava
  • 761,203
  • 64
  • 569
  • 643
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66

1 Answers1

2

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this sed to disable normal printing of unmatched lines:

sed -nE 's/^.*\[(.* 0.*)\].*/\1/p' file

Also please understand that .* is greedy in nature and due to lot of backtracking this pattern tends to get slower for large files.

I suggest using this regex with negated character class:

sed -nE 's/^[^[]*\[([^ ]* 0[^]]*)\].*/\1/p' file
anubhava
  • 761,203
  • 64
  • 569
  • 643