Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting Filename delimited by period (.)

Tags:

sh

awk

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?

like image 479
Maulzey Avatar asked Sep 25 '12 20:09

Maulzey


2 Answers

If by sh you mean bash, then

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

otherwise

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

will do, perhaps.

like image 68
Michael Krelin - hacker Avatar answered Oct 31 '22 09:10

Michael Krelin - hacker


perl:

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

sed:

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

awk:

D=`echo $c | awk -F. '{print $1}'`
like image 29
Vijay Avatar answered Oct 31 '22 09:10

Vijay