LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2012, 06:00 PM   #1
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Rep: Reputation: Disabled
print the string between ()


how can i print the string between two ().
hi I am new (abc def).

how to print "abc def"?
 
Old 10-13-2012, 06:03 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,508

Rep: Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102
We need a much better description than that. Are you working in a programming language? On the command line? Where does "hi I am new (abc def)." exist? Is it in a file? Is it in a variable?
 
Old 10-13-2012, 06:07 PM   #3
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
yeah i need a command. I tried using sed but didnt work out.I am writing script where i need to get just the strings between those 2 ()'s. i have a file that contains this kind of things.
 
Old 10-14-2012, 04:36 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
Please show us what you have tried so we may help correct it.
 
Old 10-14-2012, 05:02 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Use "grep -o" to extract the text in ()--this will include the actual (). Then filter thru SED to remove the ()
 
Old 10-14-2012, 08:04 AM   #6
NickPats
Member
 
Registered: Oct 2012
Posts: 43

Original Poster
Rep: Reputation: Disabled
should i use it like this.
grep -o "(.*)"
 
Old 10-14-2012, 09:57 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Check your same question: http://www.linuxquestions.org/questi...gs-4175432063/
And as I said, suppose you have following line in a file, named file.txt:
To: williams123@hotmail.com (Jack Williams)

Then you can cut it, using following command, if file.txt contain many different lines:
more file.txt | grep -w "To" | awk -F" " '{print $3,$4}' | sed 's/(//g' | sed 's/)//g'
Or if file has entries only like
(abc def)
(ghi jkl)
(mno pqr)

Then simply drop the "grep -w "To" and use following:
more file.txt | awk -F" " '{print $3,$4}' | sed 's/(//g' | sed 's/)//g'

Last edited by shivaa; 10-14-2012 at 10:01 AM.
 
Old 10-14-2012, 11:06 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
Well I am going to go out on a limb and ask why everyone is so keen to use several command to do such a trivial task?

I mean why grep and then sed when sed already has the ability to search?

And as for the convoluted more | grep | awk | sed | sed ... are you kidding me???

Now obviously it depends on how much data will be in between what we are looking at or if it is simple line as provided in previous post shown by meninvenus.
If we have to assume that there is other o lines, supposedly not containing 'To:' (I am guessing this is a mail log of some sort), then it could be as simple as:
Code:
awk -F"[()]" '/To:/{print $(NF-1)}' file
Even for sed it could be as simple as:
Code:
sed -n '/To:/s/^.*(\([^)]*\))/\1/p' file
Sorry for the rant but I do get a little frustrated seeing globs of commands smashed together to do a task that an individual command can do.

Edit: As per pixellany's good pick up, I forgot the second set of brackets for saving the back reference

Last edited by grail; 10-14-2012 at 10:58 PM.
 
Old 10-14-2012, 07:50 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Yes, but......

Sometimes the right answer is in fact a bit of relatively inefficient code which has the benefit of being easy to understand. If the user gets their job done with minimum effort, then what appears convoluted and inefficient might in fact be the best solution.

Obviously, there are situations where this sloppiness has no place....

In the SED example above, I think you need sed -r OR you need to escape the ( and )
 
Old 10-15-2012, 06:23 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
I decided to have a crack using cut, just to see how to do it
Code:
 echo 'To: williams123@hotmail.com (Jack Williams)'|cut -d'(' -f2|cut -d')' -f1
Jack Williams
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
capture regex and print string casperdaghost Programming 1 06-15-2012 06:18 PM
sed, print only the searched string ! noony123 Linux - Newbie 2 01-17-2011 06:09 PM
[SOLVED] search a string and print column vikas027 Programming 6 03-14-2010 09:20 AM
search string in text and print string wolfipa Linux - Software 4 09-17-2009 08:50 AM
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration