Remove non numeric values from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove non numeric values from a variable
# 8  
Old 03-05-2009
Having some more problems with my script:

A=$(sqlplus -s user/pass @lmbbroken.sql $A)
echo $A
B="no rows selected"
echo $B
if [ "$A" = "no rows selected" ]
then
echo "Empty"
else
echo "OooooOo Numbers"
left=${A%%[0-9]*}
right=${A##*[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}
echo $num
fi

The script always jumps to the else clause.
I do not understand why this is happening.
Thanks,
-Owned
# 9  
Old 03-05-2009
Quote:
Originally Posted by ownedthawte
The script always jumps to the else clause.
I do not understand why this is happening.

It happens becuase the contents of $A do not match the string you are comparing it with.

Check the exact contents with:

Code:
echo "$A" | od -c

(I would guess that there's a CR (\r) at the end of the line.)
# 10  
Old 03-05-2009
where exactly in the script should the pipe and od -c live? In the if statement? or where I am setting the A variable?
Thank you tons!
_Stan
# 11  
Old 03-05-2009
Quote:
Originally Posted by ownedthawte
where exactly in the script should the pipe and od -c live? In the if statement? or where I am setting the A variable?

Put it anywhere after $A is assigned.

Its purpose is so that you can see exactly what the string contains and adjust your script accordingly.

You may be better off using case instead of if:

Code:
case "$A" in
  "no rows selected"*) echo "Empty" ;;
  *)
     echo "OooooOo Numbers"
     left=${A%%[0-9]*}
     right=${A##*[0-9]}
     temp=${A#"$left"}
     num=${temp%"$right"}
     echo $num
     ;;
esac

# 12  
Old 03-05-2009
A is set to this:
0000000 n o r o w s s e l e c t e d
0000020 \n
0000021

B is also the same thing above.

The case example you posted did not act any different.
-Stan
# 13  
Old 03-31-2009
Hi cfajohnson,

Could you please explain what the below tags are upto ? I couldn't follow it. Thanks in advance.

A="other text here 45678754 other text here"
left=${A%%[0-9]*}
right=${A##*[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}

rgds,

Srini
# 14  
Old 03-31-2009
Quote:
Originally Posted by Srinivas_Hari
Could you please explain what the below tags are upto ? I couldn't follow it. Thanks in advance.

A="other text here 45678754 other text here"
left=${A%%[0-9]*}
right=${A##*[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}

It's explained in the Parameter Expansion section of your shell's man page.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Advanced & Expert Users

Sort by second column numeric values

From googling and reading man pages I figured out this sorts the first column by numeric values. sort -g -k 1,1 Why does the -n option not work? The man pages were a bit confusing. And what if I want to sort the second column numerically? I haven't been able to figure that out. The file... (7 Replies)
Discussion started by: cokedude
7 Replies

3. Shell Programming and Scripting

Assigning numeric values to variable

I have a code like this v_num=9 comp_num=39 if then echo "pass" fi echo "end" I am getting an error ksh: v_num=99 comp_num=39 if then echo "pass" fi echo "end" (3 Replies)
Discussion started by: swayam123
3 Replies

4. AIX

Pattern count for numeric values in aix

Hi All , I have a small code that checks pattern of digits entered in unix mode . $ echo 201202 | wc -c 7 /* output*/ When i run same command in AIX 5.1 , i am getting output with some initial blanks $ echo 201202 | wc -c 7 ... (4 Replies)
Discussion started by: Perlbaby
4 Replies

5. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

6. Shell Programming and Scripting

how to grep only particular length of numeric values

hi i have two types of file 1. temp.0000000001.data (10 digit numeric) 2. temp.000000001.data (9 digit numeric) i want to search a file which is having 10 digit numeric in between the file name. i use command like this.. ls | grep temp.^*.data but this will give both the files as... (2 Replies)
Discussion started by: somi2yoga
2 Replies

7. Shell Programming and Scripting

How to check if the file contains only numeric values

How to check if the file contains only numeric values. I don't want to read entire file it eats lot of cpu Or any way which consumes less memory n cpu.. Please suggest -S (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

8. Programming

numeric values ending in 'U'

I am getting back on the C++ programming after many years away. I recently received an SDK that has code like this where numeric values end in 'U'. What does this mean? if ((ptr % 16U) == 0U) return buffer; (3 Replies)
Discussion started by: sneakyimp
3 Replies

9. Shell Programming and Scripting

stripping out non-numeric values in a list

hi all, i'm very new to scripting and have the folllowing issue. I have used a few commands to get a list of numbers, but I need to strip away the non-numeric ones, and then need a total of all values. any ideas? root@unixserver # cat myfile | awk '{print $8}'| sort -rn 1504 1344 896 704... (2 Replies)
Discussion started by: badoshi
2 Replies

10. Shell Programming and Scripting

Replace spaces with 0's having numeric values.

What could be the regular expression with gsub function in awk to replace all numerics having spaces before to be replaced with 0s? (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question