I have a large log file I need to sort, I want to extract the text between parentheses. The format is something like this:
<@44541545451865156> (example#6144) has left the server!
How would I go about extracting "example#6144"?
I have a large log file I need to sort, I want to extract the text between parentheses. The format is something like this:
<@44541545451865156> (example#6144) has left the server!
How would I go about extracting "example#6144"?
There are many ways to skin this cat.
Assuming you always have only one lexeme in parentheses, you can use bash
parameter expansion:
while read t; do echo $(t=${t#*(}; echo ${t%)*}); done <logfile
The first substitution: ${t#*(}
cuts off everything up and including the left parenthesis, leaving you with example#6144) has left the server!
; the second one: ${t%)*}
cuts off the right parenthesis and everything after that.
Alternatively, you can also use awk
:
awk -F'[)(]' '{print $2}' logfile
-F'[)(]'
tells awk
to use either parenthesis as the field delimiter, so it splits the input string into three tokens: <@44541545451865156>
, example#6144
, and has left the server!
; then {print $2}
instructs it to print the second token.
cut
would also do:
cut -d'(' -f 2 logfile | cut -d')' -f 1
Try this:
sed -e 's/^.*(\([^()]*\)).*$/\1/' <logfile
The /^.*(\([^()]*\)).*$/
is a regular expression or regex. Regexes are hard to read until you get used to them, but are most useful for extracting text by pattern, as you are doing here.