Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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

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.

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.

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 }' filefile containes e.g.:
abcs adrfe dbea
abcs a dbea
abcd 123 dbea
abdc 1d2 dbeaoutput would be:
abcs adrfe_six_blank dbea
abcs a_six_blank dbea
abcd 123 dbea
abdc 1d2_six_blank dbeaHope it helps
NO RISK no fun
Frank

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"
;;
esacModify the above logic to evaluate a field from an input file and you have it.
Jerry

![]() |
![]() |
![]() |

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