Korn expr substr fails for non-numeric value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn expr substr fails for non-numeric value
# 1  
Old 12-22-2009
Bug 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 messages "The specific number is not valid for this command".

My goal is to read thru this file and skip items like the last one that is NOT numeric. Am I going down the wrong path? Is there a better way of handlling this task?

Thanks!
# 2  
Old 12-22-2009
Just add a simple judgement to see if the sub matches \[0-9][0-9]\ or not.
Code:
        sub=$(expr substr "$line" 4 2)
        val=$(expr "$sub" : '\([0-9][0-9]\)')
        if [[ -n "$val" ]]; then
                echo " is a number"
        else
                echo " not a number"
        fi

# 3  
Old 12-22-2009
There is no need to use expr.
Code:
#!/bin/ksh93

while read LINE
do
   sub=${LINE:4:2}
   case $sub in
      ( +([0-9]) )  echo "$sub is a number" ;;
      *)            echo "$sub is not a number" ;;
   esac
done < file

gives
Code:
12 is a number
43 is a number
53 is a number
ST is not a number

# 4  
Old 12-22-2009
How about:
Code:
num=${DDNAME:3:2}
if [[ $num =~ [0-9]+ ]]; then
  command $num
fi

# 5  
Old 12-22-2009
my ksh does not support sub=$(line:4:2);
I have to use sub=$(expr substr $line 4 2);
# 6  
Old 12-22-2009
You have to use:
Code:
sub=${line:3:2}

(curly brackets)
# 7  
Old 12-22-2009
Why I still got the error:

Code:
#!/bin/ksh
cat "num_str.txt" | while read line
do
        echo "line:$line"
        sub=${line:3:2}
        echo "sub:$sub"
        #sub=$(expr substr "$line" 4 2)
        #val=$(expr "$sub" : '\([0-9][0-9]\)')
        #if [[ -n "$val" ]]; then
        #       echo " is a number"
        #else
        #       echo " not a number"
        #fi
done

Gave me the error:
Code:
$ ksh num_str.ksh
line:E0801260
num_str.ksh[14]: : bad substitution

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expr: non-numeric argument syntax error on line 1, teletype

Hi, I tried to look up the issue i'm experiencing, but i'm confused what's wrong with my script. After executing the script I'm getting the following error expr: non-numeric argument syntax error on line 1, teletype After some research, it seems that the problem relates to bc. I have... (1 Reply)
Discussion started by: nms
1 Replies

2. 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

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

awk substr fails

Hi all, I want to get each line of a data file from position 464 plus 8 characters. I tried in two different ways, and the results were different. I'd like to know why. First method, using awk: awk '{print substr($0,464,8)}' CONCIL_VUELTA_ALF_100112_0801.okSecond method, using scripting:... (5 Replies)
Discussion started by: AlbertGM
5 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

6. Shell Programming and Scripting

expr: non-numeric argument

Hi all, i am facing the error "expr: non-numeric argument" when i use the expr command. Following is the expression which i want to execute HR=$(echo `date +%H`) MIN=$(echo `date +%M`) TOT_MIN=`expr "$HR" \* 60+$MIN` | bc echo $TOT_MIN Here I am being reported with the error expr:... (6 Replies)
Discussion started by: sparks
6 Replies

7. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: jagadeeshn04
6 Replies

8. 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

9. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

10. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies
Login or Register to Ask a Question