Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AWK script to extract a text between a pattern

Posted on 2004-07-30
9
Medium Priority
?
948 Views
Last Modified: 2008-01-09
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
Comment
Question by:a_anitha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 11682880
sed 's/[^(]*(\([^)]*\).*/\1/' input_file
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 100 total points
ID: 11684574
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
 

Author Comment

by:a_anitha
ID: 11691055
How should I provide the input file to the awk script ??
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 20

Expert Comment

by:Gns
ID: 11693614
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
 
LVL 23

Expert Comment

by:Mysidia
ID: 11787460
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
 
LVL 22

Expert Comment

by:pjedmond
ID: 11800964
..or:

cat filename | awk ' ....             etc
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question