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 05-09-2006, 02:12 AM   #1
cupoftea
LQ Newbie
 
Registered: May 2006
Posts: 19

Rep: Reputation: 0
sed help


If I were to echo 'iwant1234cupsoftea.thankyou'. How do I get sed to display only '4cupsoftea'?
 
Old 05-09-2006, 03:31 AM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
echo 'iwant1234cupsoftea.thankyou' | sed 's/^.*4cupsoftea.*/4cupsoeftea/'
 
Old 05-09-2006, 03:50 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
echo 'iwant1234cupsoftea.thankyou' | sed -e '/[1-4][1-4]*cupsoftea/s/[123]//g'
The first part of the sed command will only edit lines of text that match part of your phrase, in the case where you want to change it in a file.

I'm not sure what your general question is?
Maybe what you want is to remove all digits but the one you want.
Lets say this is a script, and $nc is the number of cups you want.
You could use sed to remove this number from a list of digits, and use that list in the main sed command.
nc=4
digitlist=$(echo 1234567890 | sed 's/'$nc'//')
echo 'iwant1234cupsoftea.thankyou' | sed 's/['$digitlist']//g'

Last edited by jschiwal; 05-09-2006 at 04:16 AM.
 
Old 05-09-2006, 04:13 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Or this one to grab whatever 1 digit in front of cupsoftea:
Code:
$ echo 'iwant1235cupsoftea.thankyou' | sed 's/^\(.*\)\([1-9]\{1\}cupsoftea\)\(.*\)/\2/'
5cupsoftea

Last edited by muha; 05-09-2006 at 04:14 AM.
 
Old 05-09-2006, 05:30 AM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
Hi Muha

You can try this:
Code:
echo "iwant1234cupsoftea.thankyou" | sed -e 's/iwant123//g' -e 's/\.thankyou//g'
 
  


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
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed Johng Linux - Software 7 09-12-2005 09:03 PM
can anyone help me on this one (sed) nukey Linux - Software 11 08-18-2005 10:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 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 07:06 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