Solved

AWK script to extract a text between a pattern

Posted on 2004-07-30
9
873 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
9 Comments
 
LVL 45

Accepted Solution

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

Assisted Solution

by:Mysidia
Mysidia earned 25 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now