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?
If by sh
you mean bash
, then
d="${c%%.*}"
otherwise
d="`echo "$d"|cut -d. -f1`"
will do, perhaps.
perl:
D=`echo $c | perl -lne 's/([^\.]*)\..*/\1/;print'`
赛德:
D=`echo $c | sed 's/\([^\.]*\)\..*/\1/'`
awk:
D=`echo $c | awk -F. '{print $1}'`
expr 命令兼容任何 sh 实现,不需要管道
d=`expr $c : '\([^\.]*\)'`