Solved

AWK script to extract a text between a pattern

Posted on 2004-07-30
9
887 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

895 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

13 Experts available now in Live!

Get 1:1 Help Now