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

Tell Numeric from Char in Unix script

Reply to Message Icon

Original Message
Name: Tom
Date: August 16, 2002 at 08:38:23 Pacific
Subject: Tell Numeric from Char in Unix script
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?


Report Offensive Message For Removal


Response Number 1
Name: Ned
Date: August 16, 2002 at 09:28:17 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 2
Name: LANkrypt0
Date: August 16, 2002 at 09:34:47 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: Tom
Date: August 16, 2002 at 12:12:27 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 4
Name: Tom
Date: August 16, 2002 at 12:15:56 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 5
Name: Frank
Date: August 19, 2002 at 01:25:52 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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


Report Offensive Follow Up For Removal


Response Number 6
Name: Jerry Lemieux
Date: August 20, 2002 at 06:39:00 Pacific
Subject: Tell Numeric from Char in Unix script
Reply: (edit)

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Tell Numeric from Char in Unix script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software