Computing.Net > Forums > Unix > Tell Numeric from Char in Unix script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Tell Numeric from Char in Unix script

Reply to Message Icon

Name: Tom
Date: August 16, 2002 at 08:38:23 Pacific
Comment:

Hi,

I am having a problem determinig how to write
an awk script that will do the following.

I have a file and in that file I am looking at a certain field. This field will contain
one of tow things. All numbers or a cobination of numbers and chars. If I see chars in the field I want to append 10 space
to the left of the field. If I see that the field has all numbers, then I ignore it and go to the next record.

I can hanlde the apeend of 10 spaces etc. What I do not know is if there is a way to tell a number from a char in a text file.

Is there a way to know that "2" is a number
and "B" is a letter?



Sponsored Link
Ads by Google

Response Number 1
Name: Ned
Date: August 16, 2002 at 09:28:17 Pacific
Reply:

One of the possible solution.

#!/bin/ksh

Let_Field=123456

if [ "$Let_Field" -eq "$Let_Field" 2>/dev/null ]
then
echo "It Is Numeric"
echo "Do Whatever you want to do"
else
echo "It Is Not Numeric"
echo "Do Whatever you want to do"
fi


0

Response Number 2
Name: LANkrypt0
Date: August 16, 2002 at 09:34:47 Pacific
Reply:

Do an awk search for [a-zA-Z]
(i.e)
/[a-zA-Z]/


0

Response Number 3
Name: Tom
Date: August 16, 2002 at 12:12:27 Pacific
Reply:

How does the awk search help?

If I am looking at a string of 7 characters
in field 3 or $3. And I search for [a-zA-z]
I can return $3 with 10 spaces.

But I only want 10 spaces at the back end of the 7 char string if chars are found. So How
do I do the if in awk?

I.E. if the filed contains "12T4567" then I want to return to a file "12T4567 "
but if the field is "1234567" I want to return "1234567". SO I just do not want to find only the rcords with chars I want all records. Just want to manipulate the ones with chars.


0

Response Number 4
Name: Tom
Date: August 16, 2002 at 12:15:56 Pacific
Reply:

Sorry the above example i response #3 should be

I.E. if the filed contains "12T4567" then I want to return to a file "12T4567 "
but if the field is "1234567" I want to return "1234567". SO I just do not want to find only the rcords with chars I want all records. Just want to manipulate the ones with chars.


0

Response Number 5
Name: Frank
Date: August 19, 2002 at 01:25:52 Pacific
Reply:

Hi Tom,

I hope I understood it correctly.
try:
awk '{ if ( $2 ~ "[a-z]" )
print $1,$2"_six_balnk",$3
else print $1,$2,$3 }' file

file containes e.g.:
abcs adrfe dbea
abcs a dbea
abcd 123 dbea
abdc 1d2 dbea

output would be:
abcs adrfe_six_blank dbea
abcs a_six_blank dbea
abcd 123 dbea
abdc 1d2_six_blank dbea

Hope it helps

NO RISK no fun
Frank


0

Related Posts

See More



Response Number 6
Name: Jerry Lemieux
Date: August 20, 2002 at 06:39:00 Pacific
Reply:

Why get awk involved in this problem? Simple pattern match can be done right in the script. Sample:

#!/bin/ksh

case $1 in

+([0-9]) ) print "It's numeric"
;;
* ) print "It ain't numeric"
;;
esac

Modify the above logic to evaluate a field from an input file and you have it.

Jerry


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Tell Numeric from Char in Unix script

Unix script line - means what?? www.computing.net/answers/unix/unix-script-line-means-what/5981.html

Unix Script - simple question pleas www.computing.net/answers/unix/unix-script-simple-question-pleas/3792.html

read from file in shell script www.computing.net/answers/unix/read-from-file-in-shell-script/3803.html