I hope someone here can help me. I have a line in a text file looking like this:
Jan 8 14:12:56 kernel: SRC=1.2.3.4 DST=255.255.255.255 LEN=104 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=44224 DPT=14000 LEN=84
I want to extract the words starting with SRC=, PROTO= and DPT=. My goal is to end up with a line looking something like this:
1.2.3.4 UDP 14000
I would prefer the solution being bash using sed, awk or similar if possible.
We can use regular expressions in python to extract specific words from a string. We can use search() method from re module to find the first occurrence of the word and then we can obtain the word using slicing.
Use Sed with groups:
sed -r 's/.*SRC=(\S+).*PROTO=(\S+).*DPT=(\S+).*/\1 \2 \3/'
One way using awk
:
awk 'BEGIN { FS = "[ =]" } { print $7, $22, $26 }' infile
Output:
1.2.3.4 UDP 14000
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