Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 957
  • Last Modified:

AWK script to extract a text between a pattern

Hi,

I have a text file , I need to extract text between a specified pattern, say open close brackets.

eg:
Input_value(myval)

I need to extract myval.

How can do this by using awk / cut etc ??

Any help will be appreciated.
0
a_anitha
Asked:
a_anitha
2 Solutions
 
sunnycoderCommented:
sed 's/[^(]*(\([^)]*\).*/\1/' input_file
0
 
MysidiaCommented:
The following awk code will read one level of markers, i.e.

Ab(
  cd)          will give "cd"

Input ab(cd(ef))  will yield cdef

awk 'BEGIN{paren = 0} ; {s = ""; for(i=1;i <= length($0); i++){ch=substr($0, i, 1); if (ch == "("){paren++} else if (ch == ")") {paren--} else if (paren > 0) {s = s ch}} if (length(s) > 0) {print s}}'
0
 
a_anithaAuthor Commented:
How should I provide the input file to the awk script ??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GnsCommented:
Assuming linux, it's not so unlikely to assume gawk... In which case we could do
# echo "ab(cd(ef))" | awk '{match($0,"\\((.*)\\)",aa); print aa[1]}'
cd(ef)
,,, which perhaps is more close to what's needed?

-- Glenn
0
 
MysidiaCommented:
To provide input to an awk script you can pipe it in from another command

like  somecommand | awk ' ... script ...'

Or supply the filename as an argument
     
    awk '... script ...'   inputfile
0
 
pjedmondCommented:
..or:

cat filename | awk ' ....             etc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now