-   Programming (
-   -   How to remove a value with sed (

mierdatuti 12-18-2008 11:01 AM

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!

fiomba 12-18-2008 12:06 PM

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

pixellany 12-18-2008 02:28 PM

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:

PTrenholme 12-18-2008 05:36 PM

Is this what you want?

pepe="hola adios xxx"
echo pepe = ${pepe}
declare pepe="$(echo $pepe | sed 's/.[^[:space:]]*[[:space:]]*\(.*$\)/\1/')"
echo pepe = ${pepe}

All times are GMT -5. The time now is 12:30 AM.