so i've got a problem. I want to remove the whole content of a file before a certain pattern has been found, but only at the first occurrence of it. the pattern : ([0-9]{2}:[0-9]{2}:[0-9]{2}).([0-9]{6})
(it is fits the date part of the string).
For example, this content:
-- 10:17:40.614568 00:00:00:00:00:00 > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 303: (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 289) 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:00:00:00:00:00, length 261, xid 0x1466f606, Flags [Broadcast]
should be parsed to :
10:17:40.614568 00:00:00:00:00:00 > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 303: (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 289) 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:00:00:00:00:00, length 261, xid 0x1466f606, Flags [Broadcast]
EDIT: Since OP mentioned condition to be met only for very first regex match in entire Input_file so adding this solution now.
awk --re-interval '
match($0,/[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}/) && !index{
print substr($0,RSTART)
index=1
next
}
index ' Input_file
Could you please try following.
awk --re-interval '
match($0,/[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}/){
print substr($0,RSTART)
}' Input_file
It will only print those lines which have a regex match found. In case you want to print those lines too which NOT have a match then do following.
awk --re-interval '
match($0,/[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}/){
print substr($0,RSTART)
next
}
1' Input_file
Since I am using an OLD awk
version so I have out --re-interval
remove it in case for you above code works(new versions of awk
it works)
Explanation of 1st code:
awk --re-interval ' ##Starting awk program from here and --re-interval enables ERE for OLD versions of awk.
match($0,/[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}/){ ##Using match utility of awk here which will check REGEX provided to it either it presents on line or not.
print substr($0,RSTART) ##match utility/function has variable named RSTART which denotes the starting of REGEX point, so I am using substring function to print from starting of that point to till end of line, since OP want to remove everything before REGEX match.
}
' Input_file ##Mentioning Input_file name here.
For 2nd code's explanation will be same as 1st code only difference is 2nd code has next
will skip lines whose regex is matched and 1
will print non-matched lines in Input_file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.