Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match pattern after first 32 letters using the grep?

Tags:

grep

awk

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?

like image 372
vishnu m c Avatar asked Sep 15 '25 05:09

vishnu m c


2 Answers

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 characters
  • 04: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
like image 71
anubhava Avatar answered Sep 17 '25 18:09

anubhava


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
like image 26
RavinderSingh13 Avatar answered Sep 17 '25 19:09

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!