LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extracting using sed (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-using-sed-924001/)

xerox 01-16-2012 02:44 AM

extracting using sed
 
Given
Code:

Example is, "Hello",
How do I extract just
Code:

Hello
using sed. Thanks.

grail 01-16-2012 02:55 AM

What have you tried?

xerox 01-16-2012 03:21 AM

Hi Grail,

You did not mention the solution.

Roken 01-16-2012 05:34 AM

sed 's/.*\(Hello\).*/\1/'

grail 01-16-2012 07:02 AM

Quote:

You did not mention the solution.
Correct ... as we are not here to do the work for you but rather assist you with being stuck. This is trivial problem which you could have solved by simply searching for
a sed tutorial.

cbtshare 01-16-2012 07:44 AM

Well actually its kinda hard for a beginner, but the point Grail is making is you should atleast put in some effort, then you'll find others more willing to help.

I tried doing it and found this works best(it took alot a reading an research lol, but glad I have alot a more useful knowledge now :)

Quote:

sed 's/^.*"\(Hello\)".*$/\1/'

David the H. 01-16-2012 02:13 PM

Another point is that you need to explain your true goal more explicitly. Do you just need to match the exact string "Hello"? Do you need to match whatever is between the quote-marks on the line (and can there be more than one set of quotes)? Do you want the last word on the line, minus any quotes? Or what?

That's the real trick to regular expressions. You must first clearly define what differentiates the part you want to match from the parts you don't want. Only then you can structure your expression accordingly.

Regex is one of the most useful tools you can learn in all computer use. It's well worth putting in some time to study them. Here are a couple of tutorials to start you with:

http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html

grail 01-16-2012 10:54 PM

And to prove David's point about how the same problem could be tackled differently depending on the requirement:
Code:

sed -r 's/^[^"]*"|".*$//g'


All times are GMT -5. The time now is 04:33 AM.