[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Unix Script file

Need help in writing, executable, run script.  
I created a file name cat >Project5.txt   I need my script to read the contents one line at a time and display the number as well;read each  file one line at a time (cat -n focusgroup.txt ).  Also need to know who to keep track of the positive, negative, and zero numbers. Display the 3 counters with a message.   I need some serious help on
#!/bin/ksh
 
input
10     went to the store
59     went to the zoo
-87    went to sleep
32     went to school
0        did nothing
12      went to Old Navy
-67     played basketball
when I run my  script i need to have the output look like this
10
59
-87
32
0
12
-67
Number of  thepositive values: 4
Number of the negative values:2
Number of the zeros values: 1
0
leftridge1
Asked:
leftridge1
  • 5
  • 2
  • 2
2 Solutions
 
wnrossCommented:
Unless this is some school assingment use awk or perl

info awk

In particular, look towards awk's reporting capabilities

http://www.vectorsite.net/tsawk.html has a tutorial (the table of contents is cryptic, but the material seems good)

Cheers,
-Bill


0
 
yuriskCommented:
Save it in a file, make it executable (chmod -u+x file.awk)
and run against input file:  ./file.awk inputfile.txt

#!/usr/bin/awk -f
# awk invocation parameter
awk -f
# first dig for digits in the strings, string by string and print found numbers,
# numbers have to be 1st non-blank char at the beginning of line (as you typed in example)

/[:digit:]/ {print $1}
# below works only 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 negative values is: ", negatives
     print "Number of zeros is: ", zeros}

0
 
yuriskCommented:
Oops, in chmod there should be 'chmod u+x', and second 'awk -f' meant to
be at the comment line above (while it works anyway :)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
leftridge1Author Commented:
My question to you is when I run the script should it be ./Project5.awk and if so will this recognize my text file as well.
#!/usr/bin/awk -f
#script Project5.scr
#extract digits in the strings, strings by string and print found numbers
/[digit:]/ {print $1}
/[:digit:]/ {print $1}
# below works only 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 negative values is: ", negatives
     print "Number of zeros is: ", zeros}

0
 
yuriskCommented:
I guess I got your question right, if not ask for further clarification.

You can name the file that contains awk program I showed whatever you want
(so , yes, Project5.scr will be OK), just remember to make it executable using
chmod u+x Project5.scr
then run it against any data file you wish (in the format you specified in example), e.g.:  
Let's say you have your data in Project5.txt   , then, assuming that Project5.txt and Project5.scr
are in the same folder, you can:
#./Project5.scr   Project5.txt

  PS It has no error-checking whatsoever, neither tries to be all cases inclusive - as it would have
complicated the script a big deal (to you).
0
 
yuriskCommented:
I see that I mixed everything (cmds and comments), here it is the right way : you'd better call this script (not changing
anything in it ) :
 #awk -f  Project5.scr   Project5.txt
0
 
leftridge1Author Commented:
I typed this in my script file
I took out usr

#!/bin/awk -f    - took out usr
#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:", positive
     print "Number of negatives values is:", negatives
     print "Number of zeros is:", zeros}

I then typed in this $ awk -f project5.scr project5.txt
this is my output  
Number of positive values is:
Number of negatives values is: 3
Number of zeros is: 1

I'm getting there it didn't show my positive  maybe because I had two 25 values nor did it show what in put file was
I want it to show what is below on the screen.

10
20
25
-80
0
-2
25
-18
Number of  the positive values: 4


I really!!!!!!! really!!!!!!!!! really!!!!!!!!!!!! appreciate this please help me
Number of the negative values:2
Number of the zeros values: 1
0
 
wnrossCommented:

You have:
END {print "Number of positive values is:", positive
     print "Number of negatives values is:", negatives
     print "Number of zeros is:", zeros}

You need:
END {print "Number of positive values is:", positives
     print "Number of negatives values is:", negatives
     print "Number of zeros is:", zeros}

Also add a "print $0" to your code to dump the input, anywhere before the END statement will do.

Cheers,
-Bill
0
 
yuriskCommented:
All the problems you report are due to errors you did while retyping my script,
below is the original script without comments, that might have confused you. And it prints
numbers ({print $1}) as well.
Next is the script, just copy&paste:


  #!/usr/bin/awk -f
 
  /[:digit:]/ {print $1}
  $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 negative values is: ", negatives
      print "Number of zeros is: ", zeros}
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now