Solved

AWK script to extract a text between a pattern

Posted on 2004-07-30
9
898 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

773 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