Computing.Net > Forums > Unix > Time difference calculation in csh

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Time difference calculation in csh

Reply to Message Icon

Name: ex
Date: September 12, 2005 at 06:09:37 Pacific
OS: UNIX
CPU/Ram: 512
Comment:

Sat Sep 10 10:00:07 2005
Sat Sep 10 16:03:02 2005
Sat Sep 10 16:40:09 2005
Sat Sep 10 17:05:44 2005
Sun Sep 11 09:06:00 2005

Thanks,
KM.



Sponsored Link
Ads by Google

Response Number 1
Name: ex
Date: September 12, 2005 at 06:53:33 Pacific
Reply:

Repost.

Hi guru's,

Please help in writing a script for calculating the time difference for the 1st and last row.

Sat Sep 10 10:00:07 2005
Sat Sep 10 16:03:02 2005
Sat Sep 10 16:40:09 2005
Sat Sep 10 17:05:44 2005
Sun Sep 11 09:06:00 2005

Thanks,
KM.


0

Response Number 2
Name: ex
Date: September 12, 2005 at 06:55:52 Pacific
Reply:

Not forgetting to mention it should be in csh, found another script that is written in ksh. Doesnt seem to work for me though. any difference?


0

Response Number 3
Name: Luke Chi
Date: September 12, 2005 at 07:59:11 Pacific
Reply:

There's no easy way to do it using Unix shell script. But, there are some on the web.

Other simple ways to do it are:

1. If you have access to Oracle, use months_between function (the returned value * 31 days * 24 hours * 60 minutes * 60 seconds).

2. If you're good at C. Writing a C program is much easier.

Luke Chi


0

Response Number 4
Name: WilliamRobertson
Date: September 13, 2005 at 00:24:55 Pacific
Reply:

Why does it have to be in csh for Heaven's sake? It is not a shell for any kind of programming.

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot

Also, if you have a script written in another shell, or awk or perl, you could call that from your csh script.


0

Response Number 5
Name: Luke Chi
Date: September 13, 2005 at 05:38:30 Pacific
Reply:

An example to use different programing languages, Unix shells, in one shell script:

Shell script:

#!/bin/sh
TIME_DIFF=123
echo "In sh, TIME_DIFF is assigned $TIME_DIFF"
#!/bin/csh
echo "In csh, TIME_DIFF = $TIME_DIFF"
#!/bin/ksh
echo "In ksh, TIME_DIFF = $TIME_DIFF"
#!/bin/sh
echo "In sh, TIME_DIFF = $TIME_DIFF"

Output:

In sh, TIME_DIFF is assigned 123
In csh, TIME_DIFF = 123
In ksh, TIME_DIFF = 123
In sh, TIME_DIFF = 123


Luke Chi


0

Related Posts

See More



Response Number 6
Name: ex
Date: September 13, 2005 at 07:17:47 Pacific
Reply:

Hi, thanks for the feedback. The script supplied by one of the guru's here below is able to calculate the difference but i dont understand the while loop portion, can anyone explain? What does the statement "while read time remainder" mean?

example:
21:20:46 102349 Audit Number Commencing
21:44:11 DAYEND Completed Check Logfiles
21:51:22 DAYEND Completed Check Logfiles
21:52:10 DAYEND Completed Check Logfiles
21:52:52 DAYEND Completed Check Logfiles
21:53:36 DAYEND Completed Check Logfiles

I need to calculate the difference between
21:53:36 to 21:20:46

#!/bin/ksh

typeset -Z2 h1 m1 s1
typeset -Z2 h2 m2 s2
typeset -Z2 hh mm ss

grep '^..:..:.. ' file.in |
while read time remainder
do

h2=$(expr "$time" : "\(..\):..:..")
m2=$(expr "$time" : "..:\(..\):..")
s2=$(expr "$time" : "..:..:\(..\)")

if [ ! "$h1" ] ; then
h1=$h2
m1=$m2
s1=$s2
fi

done

echo "from=$h1:$m1:$s1 to=$h2:$m2:$s2"

seconds=$(echo "$h2*3600+$m2*60+$s2-($h1*3600+$m1*60+$s1)" | bc)

if [ "$seconds" -lt 0 ] ; then
((seconds=seconds+86400))
fi

hh=$(expr $seconds / 3600)
mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
echo "elapsed: $hh:$mm:$ss ($seconds seconds)"

from=21:20:46 to=21:53:36
elapsed: 00:32:50 (1970 seconds)


0

Response Number 7
Name: Jim Boothe
Date: September 13, 2005 at 10:48:39 Pacific
Reply:

The read command reads one line of input into one or more variables, but the last variable gets all remaining words:

echo 'a b c d' | read w1 w2 w3
echo $w3
c d

In the case of while read time remainder, for each line of input, it reads the first word into $time and all remaining words into $remainder.  So that's a good way to capture just a single word from a line.


0

Response Number 8
Name: ex
Date: September 14, 2005 at 03:27:22 Pacific
Reply:

Hi Thanks for enlightening me. I need to proceed further to do something like that but I couldnt get it to work in ksh. Is there a different way of writing foreach loop in ksh compare to csh? Thanks for the help.

example: cat /u/path/km/a
1
2
3
4

#!/bin/ksh
foreach a (`cat /u/path/km/a`)
grep $a | ....
end

Thanks and Best Regards,
KM.


0

Response Number 9
Name: Jim Boothe
Date: September 14, 2005 at 06:46:03 Pacific
Reply:

To process on a line-by-line basis, regardless of how many words on a line, I would use:

while read a b c
  do
  echo $a $b $c
  done < infile


To process each individual word in a list or a file, I would use the for command:

for fname in $(ls *.txt)
  do
  echo "Processing $fname ..."
  done


# !/bin/ksh
for w in h e l l o
  do
  echo $w
  done


./hello.sh
h
e
l
l
o


Quotes or double-quotes will allow embedded spaces in the "words":

# !/bin/ksh
for w in "h e l l o" "w o r l d"
  do
  echo $w
  done


./hello2.sh
h e l l o
w o r l d


0

Response Number 10
Name: ex
Date: September 15, 2005 at 04:05:23 Pacific
Reply:

Thanks alot Jim. I need to ask a really newbie question.

How do I do something like that in ksh?

echo "enter the ID:"
set a $<

echo $a

this doesnt work in ksh, i need user of the script to enter some inputs.

Thanks and Best Regards,
KM.


0

Response Number 11
Name: Jim Boothe
Date: September 15, 2005 at 06:23:20 Pacific
Reply:

The \c tells echo to suppress the carriage return. This allows the user to enter the data on the same line as the prompt. But it is not required.

echo "enter the ID: \c"
read a
echo "The user entered: $a"
echo "The user entered ${#a} characters"


0

Response Number 12
Name: ex
Date: September 15, 2005 at 20:16:39 Pacific
Reply:

Hi guru's, another question because I cant get the below to work.

if (( "$mth1" == "Sep" && "$mth2" == "Aug" )) ; then
......
fi

lets say $mth1 and $mth2 are string, so how do I compare?

Thanks.


0

Response Number 13
Name: Jim Boothe
Date: September 16, 2005 at 09:11:24 Pacific
Reply:

if [ "$mth1" = Sep -a "$mth2" = Aug ] ; then
...
fi

Use = for string compare.
Use -eq -ne -gt -lt -ge for numeric compare.
Use -a to join compares with "and".
Use -o to join compares with "or".

To control your and/or groupings, you can use parentheses, but you need to escape the parentheses with backslashes and have spaces before and after:

if [ \( $a -eq 1 -o $b -gt 5 \) -a $c -lt 9 ]


0

Response Number 14
Name: Luke Chi
Date: September 17, 2005 at 08:53:03 Pacific
Reply:

*** I would like to show my respects to Jim Boothe for spending so much time to help people. ***

Luke Chi


0

Response Number 15
Name: Jim Boothe
Date: September 19, 2005 at 10:53:37 Pacific
Reply:

That's quite nice of you Luke. You are a great contributor yourself.

The effort of helping others fine tunes our own skills, right?


0

Response Number 16
Name: ex
Date: September 19, 2005 at 20:04:19 Pacific
Reply:

Hi, dont worry I am not asking question this time. Would just like to say a big Thank You for you guys for helping me along the way. This is really a great help for people who need to write simple scripts to automate some data extraction but do not have a strong UNIX background. By the way are you guys working as system administrator in your profession or just interested in UNIX. I myself is a Engineer in QA department for a US MNC.


0

Response Number 17
Name: Jim Boothe
Date: September 20, 2005 at 13:23:34 Pacific
Reply:

As for myself, I am not a unix administrator, but rather an Oracle Apps DBA.

Although this is just my observation, many unix administrators are not so strong in scripting simply because the need is not there as much. They know all about hardware, OS installing, upgrading, patching, networking, firewalls, domains, internet, and a lot of other things.

On the other hand, a DBA works a lot more within unix, managing the database and the applications and the hundreds of thousands of files and logs, and lots of cron jobs, etc, so there is a huge need for scripting.


0

Response Number 18
Name: ex
Date: September 22, 2005 at 20:37:33 Pacific
Reply:

Hi Guru's,

here comes another question.

Input:
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB FSLT Sep 11 19:32:14
CBN/C0/68/004NB SLT Sep 11 17:26:31
CBN/C0/68/004NB SLT Sep 11 17:54:40
CBN/C0/68/004NB SLT Sep 11 18:24:37
CBN/C0/68/004NB SLT Sep 11 19:09:54
CBN/C0/68/004NB SLT Sep 11 19:32:14
FA0/91/11/940NB SLT Sep 11 20:13:51
FA0/91/12/025NB SLT Sep 11 20:56:11
FA1/01/02/115NB SLT Oct 10 21:47:14

Output:
CBN/C0/68/004NB SLT Sep 11 19:32:14
FA0/91/11/940NB SLT Sep 11 20:13:51
FA0/91/12/025NB SLT Sep 11 20:56:11
FA1/01/02/115NB SLT Oct 10 21:47:14

In I have a input like that how can I convert to the output I want. I need all the lines starting with FA and only the lastest line starting with CBN assuming I can already sort the whole thing in order.


0

Response Number 19
Name: WilliamRobertson
Date: October 2, 2005 at 15:56:04 Pacific
Reply:

Just replying to a post way up above:

> An example to use different programing
> languages, Unix shells, in one shell script:
>
> Shell script:
>
> #!/bin/sh
> TIME_DIFF=123
> echo "In sh, TIME_DIFF is assigned $TIME_DIFF"
> #!/bin/csh
> echo "In csh, TIME_DIFF = $TIME_DIFF"
> #!/bin/ksh
> echo "In ksh, TIME_DIFF = $TIME_DIFF"
> #!/bin/sh
> echo "In sh, TIME_DIFF = $TIME_DIFF"
>
> Output:
>
> In sh, TIME_DIFF is assigned 123
> In csh, TIME_DIFF = 123
> In ksh, TIME_DIFF = 123
> In sh, TIME_DIFF = 123

The example as posted is a Bourne shell script with three comment lines. It does not switch shells after it has started, but just ignores lines beginning with '#'.

Only the first "#!" path is used as the interpreter for executing the script. I suspect the rule will depend on platform, but AFAIK it needs to appear as the top line. The OS then starts a process using the specified executable, as you can see from entering "ps" while the script is running.

You can confirm this by attempting to switch to a csh interpreter and execute a csh-specific command such as "setenv x 1".



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Time difference calculation in csh

Calculation of time difference www.computing.net/answers/unix/calculation-of-time-difference/7509.html

Calculating time difference www.computing.net/answers/unix/calculating-time-difference/6988.html

tab in csh www.computing.net/answers/unix/tab-in-csh/6726.html