Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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'
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
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 )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.