tags:

views:

576

answers:

4

Hi,

I have a property files that contains things like:

myhome=/home/username
lib=/home/libs

and I want to, for instance, get the home path (i.e. /home/username):

If I use cat + grep like

cat property | grep myhome

then I get: myhome=/home/username

so I could use sed to remove the 'home=', i.e. remove everything before (and including) the '='. How can I do that with sed ?

Cheers David

+2  A: 
sed -ne "s/^myhome=//p"

Answering to your comment below:

sed -ne "s/^[^=]\+=//p"
Antony Hatchkins
thanks! But is there a way of having a more generic expression where 'myhome' is not used, i.e. remove everything that before the '=' regardless of what is before '='
DavidM
updated the answer
Antony Hatchkins
actually, I just tried "s/^[^=]+=//p" and it doesn't work... it returns nothing (with -ne) or everything with (-e)
DavidM
Just tried this: ``echo "myhome=foo" | sed -ne 's/^[^=]*=//p'`` and taht results in "foo" being written on stdout. So, rplace the + with a * and all SHOULD be well.
Vatine
fixed, plus should be escaped in sed regexp. plus suggests that line `=foo` should be left as is. `*` works too.
Antony Hatchkins
+1  A: 
sed 's+.*=++g'

should do the job.

great, exactly what I wanted
DavidM
Beware: if the RHS contains a `=` this will not work.
yes, thanks luts, good to know.
DavidM
myhome=/home/username=john will be truncated to john
Antony Hatchkins
also empty lines and lines without `=` will be printed
Antony Hatchkins
+1  A: 

use awk

awk -F"=" '{print $2}' file
awk '{sub(/^.[^=]*=/,"")}1' file

or just use the shell

while IFS="="  read -r a b
do
    echo $b
done <"file"

for sed, try this

sed -ne "s/^.[^=]*=//p" file

just shell string manipulation

$ myhome=/home/username=blah
$ echo ${myhome%%=}
/home/username=blah
ghostdog74
thanks, I know awk better and thought of doing it just like you suggested, but I was trying to force myself to use/learn sed :-)
DavidM
if you know awk, there is no need to know sed. the fundamentals of sed is regex. When you learn awk, you also learn regex.
ghostdog74
I would argue that there is always a need to learn other tools... one day, you might have to deal with a project which uses sed only...and then you're stuffed
DavidM
@dmichel: True, see http://memory-alpha.org/en/wiki/Arena_(episode)
Dennis Williamson
+1  A: 

sed can do the greping for you, too.

$ sed -n -e "s/^myhome=//p" property

The -n is:

suppress automatic printing of pattern space

While the trailing p prints a matched line.