- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to Calculate help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 12:51 AM
07-15-2004 12:51 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 12:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 12:59 AM
07-15-2004 12:59 AM
Re: Script to Calculate help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 01:19 AM
07-15-2004 01:19 AM
Re: Script to Calculate help
You would have to use the command expr,
do a man expr
SOL=`expr $curr_no - $start_no`
All the best
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 01:34 AM
07-15-2004 01:34 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2004 09:26 AM
07-15-2004 09:26 AM
Re: Script to Calculate 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!