From the command line how would I extract data in file that was contained between parenthesis "()"? Awk or Grep?
Thanks in advance
Ted
From the command line how would I extract data in file that was contained between parenthesis "()"? Awk or Grep?
Thanks in advance
Ted
The answer lies somewhere here.
echo "abc(opp)aop" | sed -n 's/(.*)//p'
This will give output abcaop
and I think the request is other way around content within brackets
echo "abc(opp)aop" | sed "s/.*(\(.*\)).*/\1/
With awk:
echo "abc(opp)aop" | awk -F"[()]" '{print $2}'
echo "abc(opp)aop" | awk -F"(" '{print $2}' | awk -F")" '{print $1}'
or
echo "abc(opp)aop" |cut -d"(" -f2 | cut -d")" -f1
I never said my code is correct i said the answer lies somewhere here and i wanted the OP to try out on his own.
These solutions should do the job too, but not at the most efficient way.
How about:
awk -F')' '{print $1}' RS='(' infile
This should extract all occurrences even text that is on multiple lines between parentheses.
---------- Post updated at 11:44 ---------- Previous update was at 11:29 ----------
This variation will present multiple line matches on a single line (may be desirable)
awk -F')' '{gsub(/\n/," ",$1);print $1}' RS='(' infile
Scrutinizer,
Maybe I'm missing something, with:
awk -F')' '{print $1}' RS='('
I get this as output:
$ cat file
abc1(opp1)aop
abc2(opp2)aop
abc3(opp3)aop
$ awk -F')' '{print $1}' RS='(' file
abc1
opp1
opp2
opp3
$
And idem with:
$ awk -F')' '{gsub(/\n/," ",$1);print $1}' RS='(' file
abc1
opp1
opp2
opp3
$
One approuch for multiple line matches on a single line:
awk -F"[()]" '{for(i=2;i<NF;i+=2){print $i}}'
Franklin52, you are right:
These should work better:
awk -F')' 'FNR>1{print $1}' RS='(' infile
-and-
awk -F')' 'FNR>1{gsub(/\n/," ",$1);print $1}' RS='(' infile
Dinjo, thanks for the help. I was not able to make that work from the command line.
I am trying to do as much research on my but it can get a little confusing.
Thanks everyone that took the time to respond.
Much much appreciated.
Ted
Am sorry about that, I thought that you were giving the answer to the OP and not the hint.