Computing.Net > Forums > Unix > How to extract a string in unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to extract a string in unix

Reply to Message Icon

Name: ch Bushu
Date: May 7, 2009 at 02:47:17 Pacific
OS: Unix
Subcategory: General
Comment:

Hi,

Can any one tell me how to extract a between the brackets?

My scenario is I have three line like:
Queue(FirstQueuename)
Queue(SecondQueuename)
Queue(ThirdQueuename)

I want all the queue names to be displayed as below:

FirstQueuename
SecondQueuename
ThirdQueuename



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: May 7, 2009 at 05:22:18 Pacific
Reply:

you can use:
sed -e "s/.*(//g" -e "s/).*//g" < filename

That removes everything before ( and after )


0

Response Number 2
Name: ch Bushu
Date: May 7, 2009 at 06:16:20 Pacific
Reply:

It worked :)

Thanks a lot..Can you please tell what that command does?

How "s/.*(//g" -e "s/).*//g" seperated the string?

Thanks again...


0

Response Number 3
Name: ch Bushu
Date: May 7, 2009 at 06:42:22 Pacific
Reply:

It worked :)

Thanks a lot..Can you please tell what that command does?

How "s/.*(//g" -e "s/).*//g" seperated the string?

Thanks again...


0

Response Number 4
Name: ghostdog
Date: May 7, 2009 at 07:36:01 Pacific
Reply:

 # awk 'BEGIN{FS="[(]|[)]"}{print $(NF-1)}' file
FirstQueuename
SecondQueuename
ThirdQueuename



0

Response Number 5
Name: lankrypt0
Date: May 7, 2009 at 11:45:01 Pacific
Reply:

Sure with that statement, you actually have two replacements

the first:
s/.*(//g -- [the important part is the .*(]
matches and deletes everything from the beginning of the line up to and including the (

The second:
s/).*//g -- [the important part is the ).*]
matches and deletes everything from and including the ) to the end of the line. which leaves the stuff in the middle.


0

Related Posts

See More



Response Number 6
Name: nails
Date: May 7, 2009 at 21:26:44 Pacific
Reply:

Unfortunately, only the GNU awk supports multiple field seperators.

While the GNU awk and sed solutions are definitely the most elegant and probably the most efficient, The unix cut command can also solve this problem:

cut -d'(' -f2 < datafile | cut -d')' -f1



0

Response Number 7
Name: ghostdog
Date: May 7, 2009 at 21:32:30 Pacific
Reply:

if one doesn't have GNU awk,

awk  'BEGIN{FS="[(]"}{sub(/\)$/,"",$NF);print $NF}' file


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: How to extract a string in unix

how to pass a variable from unix www.computing.net/answers/unix/how-to-pass-a-variable-from-unix-/7649.html

how to list a directory in realtime www.computing.net/answers/unix/how-to-list-a-directory-in-realtime/8218.html

How to write a korn shell script www.computing.net/answers/unix/how-to-write-a-korn-shell-script/3643.html