Capturing text between ()


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing text between ()
# 1  
Old 06-13-2005
Capturing text between ()

Heyo all,
I'm looking for the most effective way to capture the text in between a set of parenthesis.

For example, my input is this(IBM WebSphereMQ Queue Manager Status):
QMNAME(Q1) STATUS(Ended immediately)
QMNAME(Q2) STATUS(Ended immediately)

And I would like to know how to grab just the text in between the first set of parenthesis. The text "Q1", "Q2" and so on. I've done some digging on this but have been able to find anything that works so far. I'd also be curious to know how I could capture the second instance of the parenthesis text as well. In the future I may have a use for that ability.

Thanks to anyone with any suggestions. Smilie
# 2  
Old 06-13-2005
given some assumptions....
Code:
echo 'QMNAME(Q1) STATUS(Ended immediately)' | nawk -F '[()]' 'NF>1{ for(i=2; i <= NF; i=i+2) print $i}'

# 3  
Old 06-13-2005
Quote:
Originally Posted by vgersh99
given some assumptions....
Code:
echo 'QMNAME(Q1) STATUS(Ended immediately)' | nawk -F '[()]' 'NF>1{ for(i=2; i <= NF; i=i+2) print $i}'

I don't have nawk available, but I simply changed your example to use "awk" instead and it works well. I suppose I can come up with another script to exclude the various status messages that are possible, such as "Ended Immediately" etc. to keep the list down to just the QMNAME list. Thank you. Smilie
# 4  
Old 06-13-2005
This is what I have come up with to capture only Queue Manager names and igonore "Ended immediately", as well as remove blank lines.

dspmq outputs the following text:

QMNAME(Q1) STATUS(Ended immediately)
QMNAME(Q2) STATUS(Ended immediately)
QMNAME(Q3) STATUS(Ended immediately)
QMNAME(Q4) STATUS(Ended immediately)
QMNAME(Q5) STATUS(Ended immediately)



dspmq | awk -F '[()]' 'NF>1{ for(i=2; i <= NF; i=i+2) print $i}' | sed s/Ended//g | sed s/immediately//g | sed -n -e '/^[^ ][^ ]*$/p'
# 5  
Old 06-13-2005
Try:
dspmq | sed -n '/QMNAME/s/.*(\(..\)).*/\1/p'
# 6  
Old 06-13-2005
Quote:
Originally Posted by Perderabo
Try:
dspmq | sed -n '/QMNAME/s/.*(\(..\)).*/\1/p'
This also seems to work, but doesn't seem to include a queue manager with a different name other than the "Q1" format. For example it doesn't display "TESTQUEUEMANAGER".
# 7  
Old 06-13-2005
Quote:
Originally Posted by sysera
This also seems to work, but doesn't seem to include a queue manager with a different name other than the "Q1" format. For example it doesn't display "TESTQUEUEMANAGER".
Code:
echo 'QMNAME(TESTQUEUEMANAGER) STATUS(Ended immediately)' | sed -n 's/QMNAME(\([^)][^)]*\)).*/\1/p'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing Output?

Hello All, I'm writing a Bash Script and in it I execute a piped command within a Function I wrote and I can't seem to redirect the stderr from the 1st pipe to stdout..? I'm setting the output to an Array "COMMAND_OUTPUT" and splitting on newlines using this --> "( $(...) )". By putting... (6 Replies)
Discussion started by: mrm5102
6 Replies

2. Shell Programming and Scripting

Capturing text & putting it in different variables

Dear friends, I have following text in a variable. /backup/rem1/10122010/res From this value of variable i want to take each text in different variables e.g. a=backup b=rem1 c=10122010 d=res please guide me to do so. Thank you in advance Anushree (5 Replies)
Discussion started by: anushree.a
5 Replies

3. Shell Programming and Scripting

Capturing IP address

I'm trying my first shell script. I would like my computer to launch an application if I'm on my work network, or mount my shared files if I'm on my home network. Here's how I'm trying to do it... #!/bin/bash CURRENT_IP=ipconfig getifaddr en1 HOME=192.168.1.6 WORK=192.168.0.123 if ; then... (2 Replies)
Discussion started by: bcamp1973
2 Replies

4. Shell Programming and Scripting

Capturing Particular Data

Hi everyone! Can any one help me out regarding capturing relevant data from a file. For e.g, if i want to capture the comment written after "Prompt:" for a particular date, like for this case what would be the command to capture the tag written after "Prompt:" for Date: 2009-03-20. A... (7 Replies)
Discussion started by: muhmsida
7 Replies

5. UNIX for Dummies Questions & Answers

Capturing a value in to variable.

Hi, I currently have a shell script where it captures in the value in to 'TOTAL' and outputs to a file, i want to capture another value of a query in to another variable and output it. Current script: sqlplus -s $user <<EOD | read TOTAL set heading off set pages 0 set feedback off set... (1 Reply)
Discussion started by: ravi0435
1 Replies

6. Linux

Capturing TCPDUMP

Hi, I want to capture TCPDUMP of traffic, I tried doing this but did not find success..can anyone plz correct it. # tcpdump -s0 -vv -w /home/osuresh/test_tcp_dump host 10.12.10.22 && port 161 bash: tcpdump: command not found # tcpdump -s0 -vv -w /home/osuresh/test_tcp_dump host... (5 Replies)
Discussion started by: sureshcisco
5 Replies

7. Shell Programming and Scripting

Capturing stdin

Hi all, Consider the following situation: - you launch an compiled binary application from inside a unix shell which presents a text-based type user interface where you can input information ... # echo "I am the $SHELL shell" # I am the /bin/bash shell # ./input # ... imagine the binary... (3 Replies)
Discussion started by: domivv
3 Replies

8. Shell Programming and Scripting

capturing log

Hi below is my script which copies test file to each directory in the file and checks if the permissions are ok, but I am not able to capture the log of the output. here is the script #! /usr/bin/sh set -x cd /dir/of/files while read line do cp test.txt $line rm $line/test.txt done <... (3 Replies)
Discussion started by: mgirinath
3 Replies

9. Shell Programming and Scripting

Capturing value into variable

Hi, I am new to shell scripting, I am trying to write a shell script, which will automate the process of mailing the invoices at the end of the day. For major part I am using Oracle database function. The problem is I want to capture the value returned by the Oracle function into the script... (2 Replies)
Discussion started by: prasad01
2 Replies

10. Shell Programming and Scripting

capturing process id

I am newbie to unix shells world. I am trying to capture a background process id into a file so that it can be killed later. this process is basically a java program running in background as: java TestApp & this returning process id immediately. So how can i redirect that pid into a file.... (1 Reply)
Discussion started by: bvreddy
1 Replies
Login or Register to Ask a Question