how to specify start and stop of a search string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to specify start and stop of a search string
# 1  
Old 06-18-2012
how to specify start and stop of a search string

I am trying to extract a string from a line of text. Currently I am using

grep -o 'startofstring(.........'

The string is not always the same size.
The string I'm trying to extract starts with 'test(' ends with ')'.

ex "blah,blah,blah,test(stringoftext),blah blah"

How do I specify to grep to start with 'test(' and stop with ')' ????

Also, sometimes this string exists more than once in the same text string.
How do I restrict it to only match on the first one ?

Thanks.
# 2  
Old 06-18-2012
Try:
Code:
egrep -m1 -o "[a-z]+\([^)]+\)" file | head -1

# 3  
Old 06-18-2012
Gee, what took you so long ? Smilie

That sort of works. It ends at the ')', but it does not extract the string starting at the beginning '('. I'm getting everything from teh start of the line, through to the first ')'.
# 4  
Old 06-18-2012
Can you post input that is producing undesired result and the desired output?
# 5  
Old 06-18-2012
I figured it out by tweaking the code you gave me.
I changed the [a-z] to be the beginning of the text I want.
Maybe that was your intent ?

Anyway, it works now.
Thank you very much.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:45 PM ----------

Here is how I tweaked it

egrep -m1 -o test"\([^)]+\)"

So the extracted string starts with "test(", and pulls everything up to the ")".
This User Gave Thanks to jeepguy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

2. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. UNIX for Dummies Questions & Answers

Appending a character(#) with string search at the start of the line

Hello, I have been browsing through the forum, but unable to find a solution for my requirement. I need to go through a file and search for /home/users and insert a # symbol at the start /home/users. Example output is #/home/users. Can you please help me with the awk or sed command for... (1 Reply)
Discussion started by: chandu123
1 Replies

4. Shell Programming and Scripting

read a string from its end to its start and stop when you find a specific character

How can I do this? Actually I have a file which contains a path e.g. /home/john/Music/hello.mp3 and I want to take only the filename (hello.mp3) So, I need to read the file from its end to its start till the character "/" Is this possible? Thanks, I am sure you'll not disappoint me here! Oh,... (9 Replies)
Discussion started by: hakermania
9 Replies

5. Solaris

How to start/stop processes

Please anyone tell me In my last interview the HR asks me how to monitor, start,stop & kill the various processes and subprocesses. Please anyone explain me clearly. It's my personal request (3 Replies)
Discussion started by: suneelieg
3 Replies

6. Shell Programming and Scripting

Servers Start and Stop

HI I am using below code to start and stop servers but it is not working ,how to run the script please suggest me ,if any errors in the script please let me know. #!/bin/bash IMS_START="/Webserver/AppServer/bin/startServer.sh" IMS_STOP="/Webserver/AppServer/bin/stopServer.sh" case "$1" in ... (1 Reply)
Discussion started by: RG18173
1 Replies

7. Shell Programming and Scripting

Servers Stop and Start

Hi, Every time i want to stop and start servers using PuTTY,i have to execute 6 to 10 commands every time ,i need shell script(program) for execute those commands in single command.Is it possible plz suggest me. (3 Replies)
Discussion started by: RG18173
3 Replies

8. Shell Programming and Scripting

Start Stop Restart

I'm wondering how I should make a script that can start, stop, and restart another script. What I need to be able to do, is start and stop a perl script from the command line. The easiest way of doing this seems to be to have another script, starting and stopping the other script. I have BASH,... (7 Replies)
Discussion started by: Bakes
7 Replies

9. HP-UX

ypbind start/stop

Hi, How to start or stop ypbind on HP-UX machine. Searched a little but could not find. thanks, (2 Replies)
Discussion started by: jredx
2 Replies

10. AIX

Start Stop Apache

I am in the process of reorging my Lawson db. I need to turn off the RMI server...not a problem. However my instructions also state that I must also shutdown my Servlet Container....I believe it is Apache. I have looked in /usr/apache/bin/apachectl What is the command for stopping and... (2 Replies)
Discussion started by: MILLERJ62
2 Replies
Login or Register to Ask a Question