Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-09-2006, 01:12 AM   #1
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, 02:31 AM   #2
Andrew Benton
Senior Member
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

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

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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.
digitlist=$(echo 1234567890 | sed 's/'$nc'//')
echo 'iwant1234cupsoftea.thankyou' | sed 's/['$digitlist']//g'

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

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

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

Rep: Reputation: 79
Hi Muha

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


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 12:34 AM
sed Johng Linux - Software 7 09-12-2005 08:03 PM
can anyone help me on this one (sed) nukey Linux - Software 11 08-18-2005 09:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration