Extract multiple occurance of strings between 2 patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract multiple occurance of strings between 2 patterns
# 1  
Old 10-24-2013
Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line.

For e.g. in below as input
-------------------------------------------------------------------------------------
Code:
mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)

-------------------------------------------------------------------------------------

I need output to be
-------------------------------------------------------------------------------------
Code:
hussy donald ryan johnson

-------------------------------------------------------------------------------------

condition 1 : Extract output between paranthesis.
condition 2 : number of patterns match is unknown.


I tried below command using awk, but it extracts only first pattern match.

Code:
awk -F'[(|)]' '{print $2}' filename


Last edited by Don Cragun; 10-24-2013 at 07:47 AM.. Reason: Add CODE tags.
# 2  
Old 10-24-2013
Code:
$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | perl -ne '@surnames=$_=~/\(([^)]+)\)/g;print join"\n",@surnames' -
hussey
donald
ryan
johnson

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 10-24-2013
awk solution (can be made better)

Code:
awk '{for(i=1;i<=NF;i++) {if($i~/\(/) a=a" "substr($i,1+index($i,"("))} }{gsub(/\)/,"",a);print a;a=""}' infile

This User Gave Thanks to krishmaths For This Post:
# 4  
Old 10-24-2013
To get the other fields (and print the values from each input line on a single output line) try:
Code:
awk -F '[()]' '
{       for(i = 2; i < NF; i += 2)
                printf("%s%s", $i, i == NF - 1 ? "\n" : " ")
}' filename

Note that (even though it doesn't matter with the sample input you provided) you don't want the pipe symbol in the list of characters to be treated as field separators in your -F option option-argument.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-24-2013
Try


Code:
$ echo "mike(hussey) AND mike(donald) AND mike(ryan) ANDa mike(johnson)" | awk '{gsub("[A-Za-z]*[(]|[)]*[A-Za-z]*[ ]|[)]"," ")}1'

Resulting

Code:
 hussey   donald   ryan   johnson

OR

If spacing is important then

Code:
$ echo "mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)" | awk '{gsub("[A-Za-z]*[(]","");gsub("[)]*[A-Za-z]*[ ]|[)]"," ")}1'

Resulting
Code:
hussey  donald  ryan  johnson


Last edited by Akshay Hegde; 10-24-2013 at 07:08 AM..
# 6  
Old 10-24-2013
Hi,
A sed solution:
Code:
$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | sed '/\(^[^(]*(\|)[^(]*(\|)[^(]*\)/s// /g;s/ //'
hussey donald ryan johnson

Another awk solution:
Code:
$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | awk -F'[()]' '{while((i+=2)<NF) {x=x T $i;T=" "} print x}'
hussey donald ryan johnson

Regards.
# 7  
Old 10-24-2013
Hi all ..
thank you very much for prompt reply..

@krishmaths - your answer suits my requirement perfectly
@skrynesaver- perl also works but it shows output in separate line.
@Don cragon - It works only when number of columns is known

---------- Post updated at 03:53 PM ---------- Previous update was at 03:52 PM ----------

thanks Akshay.. works perfectly
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. UNIX for Beginners Questions & Answers

Extract only first occurance

Hi All, From the below file. I need to get only the first occurrence and print. I tried to do it in separate grep not coming as expected Original file 11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;; 11001;1214;304;;;;;;;102376462.96;;;;;;;;;;;;;;;;;;;;;;;;;;... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

Extract multiple strings from line

Hello I have an output that has a string between quotes and another between square brackets on the same line. I need to extract these 2 strings Example line Device "nrst3a" attributes=(0x4) RAW SERIAL_NUMBER=SNL2 Output should look like nrst3a VD073AV1443BVW00083 I was trying with sed... (3 Replies)
Discussion started by: bombcan
3 Replies

4. Shell Programming and Scripting

awk extract strings matching multiple patterns

Hi, I wasn't quite sure how to title this one! Here goes: I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Discussion started by: chrissycc
8 Replies

5. Shell Programming and Scripting

awk? extract quoted "" strings from multiple lines.

I am trying to extract multiple strings from snmp-mib files like below. ----- $ cat IF-MIB.mib <snip> linkDown NOTIFICATION-TYPE OBJECTS { ifIndex, ifAdminStatus, ifOperStatus } STATUS current DESCRIPTION "A linkDown trap signifies that the SNMP entity, acting in... (5 Replies)
Discussion started by: genzo
5 Replies

6. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

7. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

8. Shell Programming and Scripting

extract till occurance of a string

hi , i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file. below is the extract from the file <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"... (5 Replies)
Discussion started by: sais
5 Replies

9. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

10. Shell Programming and Scripting

how to extract multiple strings from a line

Hi I have the following requirement. i have the following line from a log file one : two : Three : four : five : six : seven : eight :nine :ten Now can you pls help what i should do to get only the following output from the above line two : five : six : seven : Eight appreciate your... (3 Replies)
Discussion started by: vin_eme
3 Replies
Login or Register to Ask a Question