LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-18-2008, 11:01 AM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 39

Rep: Reputation: 15
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!
 
Old 12-18-2008, 12:06 PM   #2
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
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
 
Old 12-18-2008, 02:28 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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/
 
Old 12-18-2008, 05:36 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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}
 
  


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


All times are GMT -5. The time now is 08:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration