7

I'm writting an sh file where I have var as:

c=aabbcc.DP.09-25-2012_14_17.dmp

i want to copy only initial part in another var like:

d = aabbcc

How should I trim my var?

4

3 回答 3

11

If by sh you mean bash, then

d="${c%%.*}"

otherwise

d="`echo "$d"|cut -d. -f1`"

will do, perhaps.

于 2012-09-25T20:26:43.170 回答
8

perl:

D=`echo $c | perl -lne 's/([^\.]*)\..*/\1/;print'`

赛德:

D=`echo $c | sed 's/\([^\.]*\)\..*/\1/'`

awk:

D=`echo $c | awk -F. '{print $1}'`
于 2012-09-26T06:06:44.943 回答
2

expr 命令兼容任何 sh 实现,不需要管道

d=`expr $c : '\([^\.]*\)'`
于 2012-10-01T08:35:54.830 回答