How to print matched regex pattern using awk?
How to print svr08-core1-1654761552.278
columns?
My log files
"/usr/local/bin/publisher calllog -account='IE-00683' -uniqueid='svr08-core1-1654762416.510' -linkedid='svr08-core1-1654762416.510' -channel='SIP/IE-00683-72-000001f8' -sipcallid='3382826129@192_168_2_61' -originalsipcallid='3382826129@192_168_2_61' -src_ext='72' -dst_ext='44' -call_direction='internal' >>/var/log/asterisk/publisher_asterisk.log 2>&1 &") in new stack
CodePudding user response:
How to print matched regex pattern using awk?
match
function sets RSTART
to start of match or 0
if not match found and RLENGTH
to length of match or -1
if not match found. RSTART value is also return value. Variables set can be used with substr
to get matched string, consider following simple example, let file.txt
content be
abc 123 def
7 ghi 8
nodigitshere
then
awk 'match($0,/[0-9] /){print substr($0, RSTART, RLENGTH)}' file.txt
gives output
123
7
Explanation: match
function is used, return of which is true if match found and false otherwise, when considering as boolean. I instruct GNU AWK
to look for 1 or more (
) digits ([0-9]
) in whole line ($0
). If there is match I print
substring of whole line ($0
) starting at start of match and of length of match. Note that if line contain more 1 substring matching regex leftmost one (first) will be printed as seen in 2nd line.
(tested in gawk 4.2.1)
CodePudding user response:
I have a log file and I want to extract the following pattern from the log file.
I just want to get the following pattern.
svr08-core1-1654762440.516
[2022-06-09 09:14:00] VERBOSE[24148][C-000000e4] pbx.c: [2022-06-09 09:14:00] -- Executing [s@macro-internalcall:207] System("SIP/IE-00730-10-000001fe", "/usr/local/bin/publisher calllog -account='IE-00730' -uniqueid='svr08-core1-1654762440.516' -linkedid='svr08-core1-1654762440.516' -channel='SIP/IE-00730-10-000001fe' -sipcallid='[email protected]' -originalsipcallid='[email protected]' -src_ext='10' -dst_ext='22' -call_direction='internal' >>/var/log/asterisk/publisher_asterisk.log 2>&1 &") in new stack
CodePudding user response:
Here is a potential solution with sed
:
sed -n "s/.*\-uniqueid='\([[:alnum:]\.-]\{1,\}\)'.*/\1/p" file.log
If this doesn't solve your problem, please edit your question to include more details and/or example input and the corresponding expected output.