LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to remove a value with sed (https://www.linuxquestions.org/questions/programming-9/how-to-remove-a-value-with-sed-691507/)

mierdatuti 12-18-2008 11:01 AM

How to remove a value with sed
 
Hi,

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/"
var1=kkk

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
Code:

hola adios kkk
never change... the solution is trivial:
Code:

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...
Code:

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: http://www.grymoire.com/Unix/

PTrenholme 12-18-2008 05:36 PM

Is this what you want?
Code:

#!/bin/bash
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 11:32 AM.