开发者

sed + regular expression: how to get everything before a given character

I have a property files that contains things like:

myhome开发者_JS百科=/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


sed -ne "s/^myhome=//p"

Answering to your comment below:

sed -ne "s/^[^=]\+=//p"


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


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.


sed 's+.*=++g'

should do the job.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜