I have the following string sample:
bla bla bla [123] bla bla
I would like to extract the "123" using awk.
I thought about awk -F"[\[\]]" '{ print $1 }' but it doesn't work
Any ideas ?
I have the following string sample:
bla bla bla [123] bla bla
I would like to extract the "123" using awk.
I thought about awk -F"[\[\]]" '{ print $1 }' but it doesn't work
Any ideas ?
awk -F"[\\\[\\\]]" '{print $2}' file
Don't ask how it works, cause I have no idea
Another way, with a little less escaping:
awk -F'[\\[\\]]' '{print $2}' file
Works great - Thanks!
---------- Post updated at 09:32 PM ---------- Previous update was at 09:12 PM ----------
BTW - I am trying to capture STDOUT so hence not read a 'file' but passing the string to awk via a pipe. $0 shows the whole string but $1 shows NULL so it doesn't seem to like the delimiters.
eg. I am trying this:
STR="bla bla bla [123] bla bla" #normally the stdout from a command line call of a software product
RUNID=`echo ${STR} | awk -F'[\\[\\]]' '{print $2}'`
echo ${ID}
There's probably a better way to do it but I haven't coded for years.
any ideas?
This link might shade light explaining the difference between a regexp constant and a string constant
I ment that the escaping part is a mistery to me (why does awk need so many backslashes?). I think I'm quite good with using regexes as well as understanding them
After taking a closer look at your link, I think it really did shade a little light on that escaping part of AWK. Thanks ;]
awk -F'[][]' '{print $2}'
Try:
STR="bla bla bla [123] bla bla"
RUNID=$(echo ${STR} | awk -F'[\\[\\]]' '{print $2}')
echo ${RUNID}
BTW, if you wanted to use backticks (`) and (") you would need to do this to make it work (not counting Scrutinizer's solution):
RUNID=`echo ${STR} | awk -F"[\\\\\[\\\\\]]" '{print $2}'`
which is some terrible abomination to me
Awesome. Works a treat. Many thanks.