Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-18-2008, 11:01 AM   #1
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
How to remove a value with sed


I have this:

bash-2.03$ echo $pepe
hola adios kkk

I would like this:

pepe=adios kkk

I would like remove the first field of the pepe variable, but with a condition, I don't know the value of the fields.

I'm trying with sed but I can't:

bash-2.03$ echo $pepe | sed "s/.*\ \(.*\).*$/var1=\1/"

Could you say me how with sed?

Many thanks and sorry for my english!
Old 12-18-2008, 12:06 PM   #2
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
If the name of variable ($pepe) and its content
hola adios kkk
never change... the solution is trivial:
echo $pepe | sed "s/^hola /pepe=/"
The problem is tricker if the variable's name changes,
or you have a different content or word to substitute...,
for ex. you want to obtain...
pepe=adios kkk
Old 12-18-2008, 02:28 PM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
to operate on specific fields--where the content can vary---you are better off with something like AWK.

It CAN be done in SED.....You would have to write a Regex which matches the first group of characters that are NOT spaces, followed by a space. This seems to work, but is probably not the best solution:
sed 's/.[^ ]* //' filename
(The . is there to keep it from failing when there is a leading space.)

Really good tutorial on SED, AWK, and more here:
Old 12-18-2008, 05:36 PM   #4
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Is this what you want?
pepe="hola adios xxx"
echo pepe = ${pepe}
declare pepe="$(echo $pepe | sed 's/.[^[:space:]]*[[:space:]]*\(.*$\)/\1/')"
echo pepe = ${pepe}


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
using sed to remove a strange token jacksmash Programming 8 09-11-2008 10:20 AM
using sed to remove all but ip addresses chess Programming 10 07-02-2007 01:54 AM
help with sed to remove all text except for some Benanzo Linux - Software 7 01-04-2007 06:21 AM
how to remove path using sed dtcs Programming 2 12-25-2006 04:29 PM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 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