Link to home
Start Free TrialLog in
Avatar of leftridge1
leftridge1

asked on

Scrit problem using awk

I can't get my script to run I created a file using cat command.  Below is what I have.  Need help in calling from a command line using my file name cat my_data.txt: at the command promt I attempted to type in the following information but I couldn't please let me know what I done wrong.

$#/!/usr/bin/ksh
# Script my_practice.scr
# Exract positive, negative and zero values
zero_cnt=0
neg_cnt=0
pos_cnt=0
cat my_data.txt
if (("$1" == 0))
then
        zero_cnt++
elif ($1 < 0)
then
        neg_cnt++
elif ($1 > 0)
then
        pos_cnt++
else
        print
fi
END {print "Number of positive values is:”, positives
         print ”Number of negative values is:”, negatives
         print “Number of zeros is:”, zeros}
       
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi leftridge1,

Not sure what problems you're seeing.  I suspect though that the file is not executable.  First set the file permissions, then execute it.  Like this:

> chmod 744 my_data.txt
> ./mydata.txt



Good Luck!
Kent
Avatar of leftridge1
leftridge1

ASKER

I made the file executable.  I cant't run my program.
I'm at the prompt line and I typed in $#!/usr/bin/csh  : Event not found
I knwo what I want to run but I cant get the program to run right.  
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I' just trying to create a file with negative, positive, and zero numbers as my input, I saved the file using vi editor.  I need to call the file up in the command line.  My output should show  a message of  what I have in my input file about how many negative, positive, zero that I have like this:
$1<0 {negatives=negatives+1}
$1>0{positives=positives+1}
$1==0{zeros=zeros+1}
END{print "Summary data for awk_cnts file:"}


I think I need to call on awk, if I call on awk what about, If I do this should I have save the file as  a text file and called  it by awk
in a file make it executable by chmod -u+x file.awk
./file.awk my_practice.txt
I lost at this point
Please help a sister out!!!!!
I just figure out what I was doing wrong.  Here is the problem that I'm having with my script file:

#!/usr/bin/ksh
#Script: mypractice.scr
#Track positve, negative, and zero values along with a messages
cat my_practice.txt
if(( $1 < 0))
        then
                print "negatives=negatives +1"
if (($1 > 0))
        then
                print "positives=positives+1"
if  (($1==0))
        then
                print "zeros=zeros+1"
fi
END (print "Number of positive values is:", positives
     print "Number of negative values is:", negatives
     print "Number of zeros is:", zeros

When I call my file  ./my_practice to see if it run I get this

10
20
25
-80
0
-2
25
-18
./my_practice[15]: syntax error: `print' unexpected
Where did i go wrong
You are mixing up ksh and awk syntax.

Do you want your solution to be a ksh or awk script?  It can be done in either.
I want it to be ksh and not awk.  I think I have my if statement wrong.  In my ksh shell I insert my text file and I want my out put to look like this:

10
20
25
-80
0
-2
25
-18
Number of positive values: 4
Number of negative value: 3
Number of zeros value:1
Help!!!!!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What if I use awk script instead.  I keep getting an error.  I trying to get my script file to recognize my text file, since this is were the values are comming from.  

#!/usr/bin/ksh                                       If I use awk should this be #!usr/bin/awk -f
# Script my_practice.scr                        #look for digits in the string and print the number found
# Exract positive, negative and zero values

/{digit:]/ {print}
# works only if digit are at the beginning of l field $1, which they are
zero_cnt=0
neg_cnt=0
pos_cnt=0
cat my_data.txt    - Since I'm using awk what about this  
  if [[ $line -eq 0 ]]
     zero_cnt=$zero_cnt+1
          echo " $line = 0, $zero_cnt"
  elif [[ $line -lt 0 ]]
  then
          neg_cnt=$neg_cnt+1
          echo " $line < 0, $neg_cnt"
  elif [[ $line -gt 0 ]]
  then
          pos_cnt=$pos_cnt+1
          echo " $line > 0, $pos_cnt"
  else
          print "not sure what happened"
  fi
done

echo "Number of positive values is: $pos_cnt”
echo ”Number of negative values is: $neg_cnt"
echo “Number of zeros is: $zero_cnt"

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the confusion.  Is my cat statement ok in my script, and if so whey I run my script I want my script to see my text file for example if I run
./my_practice.scr  my_practice.txt  it should print out the following for example

0
13
-13
0
13

Number of positive values: 2
Number of negatives values:1
number of the zeros values:2
Thanks, for all your help
awk '($1~/^-/){n++}($1=="0"){z++}{p++}END{print "negative: "n;print "zero: "z;print "positive: "p}' my_practice.txt
I'm still getting error message saying ./my_practe.scr [10]: syntax error: 'then unexpected
when I run my script I typed in :
./my_practice.scr my_practice.txt and i got the error message below
Hi leftridge1,


  if [[ $line -eq 0 ]]
     zero_cnt=$zero_cnt+1

needs to be:

  if [[ $line -eq 0 ]]
  then
     zero_cnt=$zero_cnt+1


Hi ahoffman,

You're just entirely to comfortable with regular expressions.  :)


Kent

I typed everthing in like below


#!/usr/bin/ksh
#Script Project5.scr
#Extract positive, negative and zero values
zero_cnt=0
neg_cnt=0
pos+cnt=0
cat my_practice| while read line
do
if [[$line -eq 0]]
then
   zero_cnt=$zero_cnt+1
   echo "$line =0, $zero_cnt"
elif [[$line -1t 0]]
then
   neg_cnt=$neg_cnt+1
   echo "$line <0, $neg_cnt"
elif [[$line -gt 0]]
then
   pos_cnt=$pos_cnt+1
   echo "$line >0, $pos_cnt"
else
   print"not sure what happended"
fi
done
echo  "Number of positive values is: $pos_cnt"
echo "Number of negative values is: $neg_cnt"
echo "Number of zeros is:$zero_cnt"
print "Number of zeros is:", zeros
./my_Project5.scr: Command not found.
I made both of the file executable
chmod 744 my_practice.txt and Chmod 744 my_practice.scr and I still get an error saying project not found

File names in unix are case sensitive.

You've got an upper case 'P' in the command line, but suggest otherwise that the name is all lower case.


Kent
> You're just entirely to comfortable with regular expressions.  :)
what does this mean?
I guess that my simplistic solution works flawless ;-)















































#!/usr/bin/awk -f
#script project5.scr
#extract digits in the strings, strings by string and print found numbers
/[:digit:]/ {print}
#below will work if digits are at the beginning of lines, so $1 means 1st field
#in the input string, then it checks its sign incrementing sign counters
$1<0 {negatives=negatives+1}
$1>0 {positives=positives+1}
$1==0 {zeros=zeros+1}
END {print "Number of positive values is:, positives
     print "Number of negatives values is:, negatives
     print "Number of zeros is:", zeros}
I typed in         $ awk -f project5.scr  project5.txt
awk: project5.scr:10: END {print "Number of positive values is:, positives
awk: project5.scr:10:            ^ unterminated string

and this is the error that I'm getting
> awk: project5.scr:10:            ^ unterminated string
dooh, you got an awk which has more error messages than the simplistic "awk is bailing out", just read them ;-)
 your print commands miss the closing quote