LinuxQuestions.org
Review your favorite Linux distribution.
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 01-16-2012, 03:44 AM   #1
xerox
Member
 
Registered: Mar 2011
Posts: 43

Rep: Reputation: 0
Unhappy extracting using sed


Given
Code:
Example is, "Hello",
How do I extract just
Code:
Hello
using sed. Thanks.
 
Old 01-16-2012, 03:55 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
What have you tried?
 
1 members found this post helpful.
Old 01-16-2012, 04:21 AM   #3
xerox
Member
 
Registered: Mar 2011
Posts: 43

Original Poster
Rep: Reputation: 0
Hi Grail,

You did not mention the solution.
 
Old 01-16-2012, 06:34 AM   #4
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
sed 's/.*\(Hello\).*/\1/'
 
Old 01-16-2012, 08:02 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
1 members found this post helpful.
Old 01-16-2012, 08:44 AM   #6
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
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/'
 
Old 01-16-2012, 03:13 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
1 members found this post helpful.
Old 01-16-2012, 11:54 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And to prove David's point about how the same problem could be tackled differently depending on the requirement:
Code:
sed -r 's/^[^"]*"|".*$//g'
 
1 members found this post helpful.
  


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
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 08:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 08:43 AM
extracting part of a string (sed, bash, ...) rama76 Programming 3 01-11-2008 09:14 AM
Extracting data from file using sed EneWolverine Programming 7 12-29-2006 10:23 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 04:18 PM.

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