help required for 'expr substr' function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help required for 'expr substr' function
# 1  
Old 02-24-2010
Data 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
# 2  
Old 02-24-2010
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
# 3  
Old 02-24-2010
Do not use spaces around "=" and use $() (or if you have to ``) when assigning values to variables.
e.g.:

Code:
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
Code:
read m1 < reporting_date.txt
for file in GMG_BASEL2*.txt
do
  year=${m1##*/}
  echo $year
done

# 4  
Old 02-24-2010
Code:
year=`expr substr $m1 7 4`


cheers,
Devaraj Takhellambam
# 5  
Old 02-24-2010
You missed `[backdic] symbol in your expression.
Try this,

year=`expr substr $m1 1 2`
# 6  
Old 02-24-2010
MySQL Use backtick

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.

Code:
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.
# 7  
Old 02-24-2010
Computer thanks

thanks a lot
i used the first approach & it worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

2. Shell Programming and Scripting

/usr/local/bin/expr function not working

Legends, I am not able to set "expr" function in ksh script. Below is the sample code i used, and output is as "Syntax error" Please help me to come out of it. OUTPUT (9 Replies)
Discussion started by: sdosanjh
9 Replies

3. Shell Programming and Scripting

Error with expr - "expr: syntax error"

Hi All, I'm writing a shell script in KSH, where I want to store the filename, total record count and actual record count of all the source files. The source files reside in 4 different sub-folders under the same root folder. Below is code: #!/usr/bin/ksh... (6 Replies)
Discussion started by: jagari
6 Replies

4. Shell Programming and Scripting

Korn expr substr fails for non-numeric value

I am running AIX 5.3 using the Korn Shell. I am reading file names from a file, as an example: E0801260 E0824349 E0925345 EMPMSTR statement "num=$(expr substr "$DDNAME" 4 2) extracts the numeric values fine. But when I het the last entry, it returns num=MS, but I get an error... (19 Replies)
Discussion started by: kafkaf55
19 Replies

5. Shell Programming and Scripting

test expr VS [ expr ]

What is the difference between test expr VS . For example : if test 5 -eq 6 echo "Wrong" and if echo "Wrong" bot will give the same output as Wrong. Now, what is the difference between these two? though they are producing the same result why we need two? Any answer will be... (2 Replies)
Discussion started by: ashok.g
2 Replies

6. Shell Programming and Scripting

The function of substr clause in awk command

Hello all, Please help me in letting me know the function of *substr* function in awk... actually i am new with this function i can play with awk but for this function needs your help in making me understand the correct way of using it. I am writting a code please advice regarding this... (4 Replies)
Discussion started by: jojo123
4 Replies

7. Shell Programming and Scripting

use of substr function in awk

i want to get substring of second coloum of an file using awk substring function.please help me out (2 Replies)
Discussion started by: RahulJoshi
2 Replies

8. Shell Programming and Scripting

Getting required fields from a test file in required fromat in unix

My data is something like shown below. date1 date2 aaa bbbb ccccc date3 date4 dddd eeeeeee ffffffffff ggggg hh I want the output like this date1date2 aaa eeeeee I serached in the forum but didn't find the exact matching solution. Please help. (7 Replies)
Discussion started by: rdhanek
7 Replies

9. UNIX for Dummies Questions & Answers

substr function in perl

Hi friends, I have written a perl code and it works fine but I am not sure tommorow it works or not, please help me. problem : When diff is 1 then success other than its failure but tomorrow its 20090401 and the enddate is 20090331. thats why I write the code this type but it does not work and... (1 Reply)
Discussion started by: tukuna82
1 Replies

10. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies
Login or Register to Ask a Question