I was trying to filter lines with pattern 04:26. I expected the command,
cat file1.txt | grep -E '04:26'
to filter the lines which contain 04:26 after timestamps. Instead, I got the second line also.
file1.txt
2022-12-23T04:26:47.748412+00:00 raspberrypi dnsmasq-dhcp[698]: DHCPREQUEST(eth0) 192.168.42.17 04:c8:07:23:04:26
2022-12-23T04:26:47.749307+00:00 raspberrypi dnsmasq-dhcp[698]: DHCPACK(eth0) 192.168.42.17 04:c8:07:23:34:13
How to mask the first 32 letters of timestamps from matching?
You may use this grep
:
grep -E '^.{32,}04:26' file
2022-12-23T04:26:47.748412+00:00 raspberrypi dnsmasq-dhcp[698]: DHCPREQUEST(eth0) 192.168.42.17 04:c8:07:23:04:26
Breakdown:
^
: Start.{32,}
: Match 32 or more characters04:26
: Match 04:26
Alternatively you can use this grep
as well:
grep ' .*04:26' file
Considering the fact that you want to ignore timestamp text that is before first space in each line.
An awk
solution:
awk '$NF ~ /04:26/' file
With your shown samples please try following awk
code. Simple explanation would be, setting field separator to 32 characters from starting of line, then in main program checking if 2nd field is matching everything till :
followed by 04:26
if this condition matches then print that line.
awk -F'^.{32}' '$2~/^.*:04:26/' Input_file
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