[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Scrit problem using awk

Posted on 2006-04-30
19
Medium Priority
?
335 Views
Last Modified: 2010-05-18
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}
       
0
Comment
Question by:leftridge1
  • 9
  • 5
  • 3
  • +2
19 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16574768
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
0
 

Author Comment

by:leftridge1
ID: 16574869
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.  
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 16575334
#!/usr/bin/ksh
is not what you type at the prompt
#!/usr/bin/ksh
would be  the first line of the file
./my_practice.scr
or whatever the name of the file is, would be what you type at the prompt

But the program has a few errors
if (("$1" == 0))
should be
if (( "$1" == 0))
" is unmatched in
print "Number of positive values is:”, positives
END{
looks more like awk syntax than ksh syntax

What is your program supposed to do?
And should it be be a ksh program, which the first part looks like, except for the ++
or an awk program which the last part looks like, except for the  ” and  “
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:leftridge1
ID: 16576674
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!!!!!
0
 

Author Comment

by:leftridge1
ID: 16578127
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 16581484
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.
0
 

Author Comment

by:leftridge1
ID: 16581680
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!!!!!
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1800 total points
ID: 16582266
Hi leftridge,

A couple of things to address here.  The first is that when you run the script you're seeing the contents of the file because you're executing cat to send the file to the terminal.  That's fine, but it doesn't put the contents anywhere that you can actually use the data.

Also, this is shell programming, not C.  You need to follow the shell syntax.  :)

Here's your script with some changes.  It gets you a long way toward where you're trying to go, but still needs a couple of things to make it complete.


Good Luck!
Kent


#!/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 | while read line
do
  if [[ $line -eq 0 ]]
  then
          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"
0
 

Author Comment

by:leftridge1
ID: 16585289
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"

0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1800 total points
ID: 16585352
Hi leftridge,

Where are you using awk?  

The cat statement still sends all of the data to the terminal and not to a process.

0
 

Author Comment

by:leftridge1
ID: 16585862
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16586016
awk '($1~/^-/){n++}($1=="0"){z++}{p++}END{print "negative: "n;print "zero: "z;print "positive: "p}' my_practice.txt
0
 

Author Comment

by:leftridge1
ID: 16586350
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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16586369
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

0
 

Author Comment

by:leftridge1
ID: 16587921
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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16588257

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
0
 
LVL 51

Expert Comment

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

Author Comment

by:leftridge1
ID: 16589645















































#!/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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16591487
> 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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question