1832921 Members
3133 Online
110048 Solutions
New Discussion

Script to Calculate help

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

Script to Calculate help

Hi,

I am stuck with how to go through the calculation to read 900 lines out of a log file.

Due to the tail command only getting 20k of lines, I need about 900 lines to monitor what's going on. Therefore I have one script as follows:

---------------------------
#!/bin/sh
LOG=/disk1/abc.log

curr_no=`wc -l $LOG|awk '{print $1}'`
start_no=900

sed -n '$curr_no - $start_no,/$curr_no/p' abc.log>a1
---------------------------

curr_no is the total number of lines on abc.log.
start_no is 900 lines.
$curr_no - $start_no is the difference of total-900.

I don't know how to convert string back to number, as "$curr_no - $start_no" is getting string only.

Hope someone help, or give some other direction.

Very appreciated.

Steven
Steve
5 REPLIES 5
Franky_1
Respected Contributor
Solution

Re: Script to Calculate help

Hi Steven,

why don't you just use

cat |while read line
do

.
.
done

HTH

Franky
Don't worry be happy
RAC_1
Honored Contributor

Re: Script to Calculate help

This is crude method, but will work.

total= `nl "log_file"|tail -1|awk '{print $1}'`
#Display last 900 lines.

start_line=$(($total-900))

nl "log_file"|sed -n '/$start_line/,/$total/p;'

Anil
There is no substitute to HARDWORK
Victor BERRIDGE
Honored Contributor

Re: Script to Calculate help

Hi Steven,
You would have to use the command expr,
do a man expr

SOL=`expr $curr_no - $start_no`

All the best
Victor
Hein van den Heuvel
Honored Contributor

Re: Script to Calculate help


If the file is really really big, such that wc takes significant time, then you just want to make a single pass. The following awk one liner will do just that.

awk -v W=900 '{line[i++%W]=$0} END {j=i-W;while (j
It works by putting every line seen in a 'circular' array of size W (900), overwritting as it needs to. At the end, print the lines in the array. The circular effect is achieved using a MODULUS (%) function.

Hein.


With protection against short files...

awk -v W=3 '{line[i++%W]=$0} END {j=(i>W)?i-W:i;while (j
Steven Chen_1
Super Advisor

Re: Script to Calculate help

Thanks all for the help.

Victor's "expr" works great. Hein's "awk" is very great structually speaking. But awk runs for 7 seconds while expr way runs 2 seconds.

Please see the followings:

1)
-----------------
#!/bin/sh
LOG=/disk1/abc.log

curr_no=`wc -l $LOG|awk '{print $1}'`
start_no=`expr $curr_no - 900`

sed -n "$start_no,$curr_no p" $LOG |more
------------------

2)
*******************
#!/bin/sh
LOG=/disk1/abc.log

awk -v W=900 '{line[i++%W]=$0} END {j=i-W;while (j| more
********************

Again, thanks a lot!

Steve