help required for 'expr substr' function

hi

iam trying to extract a certain portion of the string whose value is stored below,but am getting syntax eror.The command is shown below

for file in GMG_BASEL2*.txt
do
m1= cat reporting_date.txt
year= expr substr $m1 1 2
echo $year
done

m1 has date 10/31/2009
but this vale is not passed to next variable where am using substr function.
can some one please help on this?

Thanks in advance

If you want to extract a certain portion of the string you can use the following way.
Example : s= "this is for testing"
echo ${s:12:18}

this will extract the word testing from the string and will output "testing"
Explanation : From the string 's' we are extracting from 12th character to the 18th character

Do not use spaces around "=" and use $() (or if you have to ``) when assigning values to variables.
e.g.:

for file in GMG_BASEL2*.txt
do
  m1=$(cat reporting_date.txt)
  year=$(expr substr $m1 7 4)
  echo $year
done

or, using some alternatives

read m1 < reporting_date.txt
for file in GMG_BASEL2*.txt
do
  year=${m1##*/}
  echo $year
done
year=`expr substr $m1 7 4`

cheers,
Devaraj Takhellambam

You missed `[backdic] symbol in your expression.
Try this,

year=`expr substr $m1 1 2`

Hello Mr.Jagadeshn,

You made some simple mistakes there . You tried to assigned the result of cat to a variable.Here you need to execute the cat using \` \`.

m1=`cat file`

If m1 has the string then the first two characters of the m1 will assigned to the $year.

In fact the year value is starting from the 7th character.

m=`cat file`
year= expr substr $m 7 5
echo $year

Consider file has the value 10/31/2009.So now the year will have 2009.

Thanks.

thanks a lot
i used the first approach & it worked.