ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Actually the message i posted was just one line of my code.I thought,only that line was the reason of error.Now,i think something other than that line casues error.I'm sending the full code.
Note:The aim of this code is to analize output of unix sar command.I'm going to work on -c,-r and -u commands.I'm asked to find miminum,maximum and average of each column of sar output.I'm working on sar -c command now.I just want you to help me to find the average of "proc/s" column.
This rapidshare thing is a massive pain in the backside. needs javascript, presents captchas, bombards me with "buy premium service", ugly and confusing...
Please consider pastebin or nopaste. Or if it's not too long, just post in [code] tags in the thread.
I'd say that this is really the sort of task which is much better accomplished using Perl:
Code:
#!/usr/bin/perl -w
use strict;
my $date = "";
my $total = 0;
my $count = 0;
while(<>) {
# remove the \n at the end of the line.
chomp;
if ( $. == 1 ) {
# substitute everything up to the last space with nothing
s/^.* //;
$date = $_;
next;
}
if ( /^\d+:\d+:\d+\s+(\d+\.\d+)$/ ) {
$total += $1;
$count++;
}
}
printf( "date=$date; total=$total; count=$count; mean=%5.3f\n", ($total/$count) );
Feed the sar lofgile into it in stdin, or specify the filename on the command line.
Line 9: using cut like this to extract the date simply isn't going to work properly. The file ornek2-c.txt contains many lines. You'll end up with all of them in the output. You already have this line in the myline variable, so you should use this as input, not the whole file. There is a shell variable expansion modifier which will chop off the leading characters of a variable, and you can use glob-style patterns in this. Since the date is preceded by the tab character, you can use this:
Code:
echo "The date is ${myline##*<TAB>}"
Where <TAB> is a real tab character. However, I suspect there could be cases where there is no tab character depending on the length of the host name. To be safe it might be better to use awk with myline as the input:
Code:
dt=$(echo "$myline" | awk '{ print $NF }')
echo "The date is $dt"
Line 17: there's no need to invoke an external command (expr). The shell can do integer arithmatic quite OK:
Code:
let i+=1
or
Code:
i=$(( $i + 1 ))
according to your tastes.
Lines 12-15. What's with all these temporary files. They are slow, and risky. Don't create temporary files unless you need to. Instea, I'd want to store the count so far in a variable. Declare the variable before the while loop starts, and 0 it. Then it becomes a two stage process: 1. extract the number from the lines you want, 2. add the number. Extracting could be done like with the date example above (be sure to exclude the line with the average!). The addition will need to be done with bc since bash doesn't support floating point math internally. By the way, zsh does support floating point math, which would make your script a lot nicer. You provided a good example of using bc in the original post.
You should keep a track of the count of items you've added to your total. Calculating the mean is then very simple.
...BUT shell is really not ideal for this sort of number crunching. Having to invoke an external program for every line of input will lead to woefully bad performance for larger input files. The shell is good for a lot of stuff, but really not this sort of thing. Perl or awk are more ideal and installed on most any unix-like OS you will encounter.
I assume since bash was mandated this is a sort of homework (Hence me not posting a complete script). Get it working in bash, but tell them it is not ideal for this sort of tasks for the reasons I mentioned, and any more you can think of. You might even get extra marks. That or you'll be marked down for making the person who set the problem look silly. Ho hum.
...BUT shell is really not ideal for this sort of number crunching. Having to invoke an external program for every line of input will lead to woefully bad performance for larger input files. The shell is good for a lot of stuff, but really not this sort of thing. Perl or awk are more ideal and installed on most any unix-like OS you will encounter.
I assume since bash was mandated this is a sort of homework (Hence me not posting a complete script). Get it working in bash, but tell them it is not ideal for this sort of tasks for the reasons I mentioned, and any more you can think of. You might even get extra marks. That or you'll be marked down for making the person who set the problem look silly. Ho hum.
if i was not restricted with language,i would prefer c,c++ or java.but our dear research assistants are not tolerant about their homeworks(i hope they won't read my post ).And about my homework: in another postmakyo helped me to fix my bash problem.Thank you very much about your interest,sharing your knowledge and spending your time with my question.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.