V
V
Vadim Pirx2016-06-07 10:04:26
bash
Vadim Pirx, 2016-06-07 10:04:26

How to bite a substring from a string?

I am writing a shell script. it searches the logs for the lines I need, then I need to bite the substring I need from the line into the file.
here is the line:

May 31 04:12:10 SSCK01 dhcpd[16691]: [ID 988538 local7.notice] [tid:30] NTCE DHCPOP(101) Protocol: DHCPDiscover from STRING-K01 PON 1/1/02/01:12.1.1 (chAddr=00:13:77:6d:44:6b) via 81.91.202.1.

here it is necessary to bite out:
STRING-K01 PON 1/1/02/01:12.1.1
I first bite out with awk {print $13,$14,$15} but then I noticed that the number of fields changes in other lines and bites out not what is needed.
I think it's right to bite on a regular expression:
grep -E -o "\b[STRING-]+[X00,K01-99]+" "+[PON]+" "+[1/1/01-09/01-09/:01-64.1.1]\b"

but this design throws an error.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2016-06-07
@pirxon

if " STRING-" and " PON " are defining then you can do this:

$ awk -F" STRING-| PON " '{print "STRING-"$2" PON "$3 }' | awk '{print $1" "$2" "$3}'  fileIn >> fileOut

V
Vladimir Kuts, 2016-06-07
@fox_12

cat your_log | perl -e 'while(<>){ if ($_ =~/.*from\s+(STRING.*)\s+\(chAddr/){ print $1."\n"; }}'

or so
cat your_log | grep -E -o 'STRING-K\d{2}\s+PON\s+\d\/\d\/\d{2}\/\d{2}:\d{2}\.\d\.\d'

V
Vadim Pirx, 2016-06-07
@pirxon

cat your_log | grep -E -o 'STRING-K\d{2}\s+PON\s+\d\/\d\/\d{2}\/\d{2}:\d{1,2}\.\d\.\d'

found an online regex editor. This regularka is correct, but for some reason it didn't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question