LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 02: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, 02:55 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What have you tried?
 
1 members found this post helpful.
Old 01-16-2012, 03: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, 05:34 AM   #4
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

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

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 07:44 AM   #6
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

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, 02:13 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 10:54 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
extracting part of a string (sed, bash, ...) rama76 Programming 3 01-11-2008 08:14 AM
Extracting data from file using sed EneWolverine Programming 7 12-29-2006 09:23 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

All times are GMT -5. The time now is 11:43 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
Open Source Consulting | Domain Registration