Work around for a mathmatical assignment


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Work around for a mathmatical assignment
# 1  
Old 05-26-2005
Work around for a mathmatical assignment

Hi All,
I want to a mathematical callculations as follow:

I have a file FILE1
I want to do following assignment

COUNTER1=`grep -n $string1 FILE1 | cut -d : -f1`
This will give me number of the line on which the string occured.

COUNTER2=`grep -n $string2 FILE1 | cut -d : -f1`
Same as COUNTER1

let i=$COUNTER1-$COUNTER2

It gives an error saying syntax error

Can anyone help on this?

Thanks
VEN
# 2  
Old 05-26-2005
You never said more about what the syntax error said.

Possible reason for the syntax error is multiple occurences of the string1 or string2 which are found.

Check out this code.

It will find 1 occurence for each string1 and string2 and then go ahead.

Code:
#! /bin/sh

COUNTER1=`grep -n $string1 FILE1 | cut -d : -f1`
COUNTER2=`grep -n $string2 FILE1 | cut -d : -f1`

i=$((COUNTER2 - COUNTER1))

echo " Difference of $COUNTER2 and $COUNTER1 is $i "

man grep will help you with other grep options.

Vino
# 3  
Old 05-28-2005
Hi Vino,
I have tried that but even that is not working.
There are no multiple occurences of the string and each grep statement will return only one line.
One thing that amzes me about these mathematical calculations is:
i=10
j=5
let k=$i-$j
This code works fine.
even:
i=`cat file1 | wc -l`
j=`cat file2 | wc -l`
let k=$i-$j
Even this code works.
What problems it has with GREP???
In general I want to know about VARIABLE HANDLING in UNIX script.

I think the root cause of this issue lies in the way UNIX handles variables.

Thanks
Abhijit.
# 4  
Old 05-28-2005
Could you post the exact syntax error and possibly the script you are executing.

Vino
# 5  
Old 05-28-2005
Quote:
Originally Posted by VENC22
let i=$COUNTER1-$COUNTER2

It gives an error saying syntax error
Code:
#!/bin/ksh

# other code here

# note the quoting
let i="$COUNTER1 - $COUNTER2"

# more code

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies

2. UNIX for Dummies Questions & Answers

Need a little help with assignment

Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions: Step 4: Modify your script so that if there is an argument called TestError you display the following error message with your usage statement. TestError found Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies

3. Shell Programming and Scripting

My script work on Linux but not work in sunos.

My script work on Linux but not work in sun os. my script. logFiles="sentLog1.log sentLog2.log" intial_time="0 0" logLocation="/usr/local/tomcat/logs/" sleepTime=600 failMessage=":: $(tput bold)Log not update$(tput rmso) = " successMessage="OK" arr=($logFiles)... (7 Replies)
Discussion started by: ooilinlove
7 Replies

4. Homework & Coursework Questions

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory · By modification time when called with lm · By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a shell script which... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

5. Shell Programming and Scripting

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory · By modification time when called with lm · By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

6. Homework & Coursework Questions

Assignment Help

1. List commands to create the directory hierarchy $HOME/a/b/c in vi to replace all occurences of TMP with tmp in lines 1 through 10 in vi to replace first occurence of CPU_file with DISK_file at line 15 2. Explain with a very simple example, usage of "ls -a" 3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies

7. Shell Programming and Scripting

RE value assignment

Hi all How do I assign a pattern to a variable after a match is found using a regular expression in PERL? For example using a regular expression (RE) and matching as given if ($_ =~ /(?:\s*+\s*,)*\s*+\s*/) I want to assign the pattern matched by the RE to a variable. e.g. given the... (4 Replies)
Discussion started by: my_Perl
4 Replies

8. Shell Programming and Scripting

mathmatical expression

Hi All, How can I execute the below statement using an awk or "expr"?? time=14400 hours=2 mins=160 secs=`expr((((time/ 3600) - hours) * 60) - mins) * 60 Thanks in advance JS (5 Replies)
Discussion started by: jisha
5 Replies

9. What is on Your Mind?

New Assignment

All Sys Administrators, With due respect I would like to know what should be BEST Things to do when LEAVING one job , and what Precaution MUST be taken while taking over new JOB?? Please Discuss in detail the STEP to be taken for both the TIME ?? (3 Replies)
Discussion started by: vakharia Mahesh
3 Replies

10. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies
Login or Register to Ask a Question