I keep getting lost. You have shown several different bits and pieces of your code and commented that you have made other changes without showing them to us. The code below makes some guesses at what you want based on my understanding from re-reading all of the posts in this thread.
You still refuse to show us the format you want for the sums and averages of the times the system has been running for the last 7 and 30 entries in the log file. So, I have arbitrarily decided to print those values in the following format:
where n is the number of lines used from the end of the file to perform the calculations. (If there are less than 7 or 30 lines in the log file, the output will tell you how many lines were actually used. The sums are shown in hours and minutes. The averages are shown in hours, minutes, and seconds.)
There are several things that still make absolutely no sense to me, but I think the script below does what you asked for (even if it doesn't make sense to me).
First some notes before we get into the code:
If there is any chance that a script can be run close to midnight, it is dangerous to make multiple calls to the date utility to gather bits and pieces of various date components. The code below only invokes date once and extracts all of the date information used in the script from that one invocation.
Even if a machine is just up for 4 days and 4 hours, the substring extraction you're using from the uptime output will not work. (Output in this case from uptime could be something like 105:19 (for 4 days, 9 hours, and 19 minutes) and your current code would put 105:1 in your log file.) The code below puts just the minutes (without the "min" or "mi") or the complete hours and minutes in log file entries.
I repeat that using /home/uplog.txt instead of $HOME/uplog.txt will fail if more than one user on a system runs this script unless they are running with root priviledges.
I repeat that the number you are extracting from the uptime output says absolutely nothing about how long a given user has been working on a project, how long they have been logged in, or anything else related to any particular user. The number you are using just tells you how long it has been since the machine you are using was rebooted. I see absolutely no reason why adding or averaging 7 or 30 of these values would be important to anyone!
This script uses echo to add a line to the end of your log file. The awk script then processes that log file. If, and only if, the log file contains more than 200 (or maxrec) lines, the awk script will rewrite the log file in place throwing away all but the last maxrec lines. Until you are confident that this works correctly, make a backup copy of your log file between the echo that adds a line to the log file and the awk script that rewrites the log file and writes the averages file.
This awk script should work for the up time formats you have described for your version of the uptime utility. (The BSD and OS X versions of the uptime utility provide considerably more complex ouput formats than you have described for your version of uptime. This script will not work correctly with the BSD or OS X versions of uptime.)
This awk script uses two circular buffers. The array l[] holds up to maxrec lines from the log file. After maxrec lines have been read, the next input line overwrites the oldest input line so when we get to the end of file, l[] holds only the last maxrec lines that were in the file. The array mins[] holds up to 30 up time values (after converting them to minutes) from the last 30 lines read from the log file.
The 7 and 30 for the number of lines to be averaged and the 200 for the number of lines to be kept in the log file are parameterized, so you just need to change the number used to initialize nl_av1, nl_av2, and maxrec, respectively, to have the script use whatever values you want. It will automatically adjust such that nl_av1 and nl_av2 will never be greater than maxrec.
The averages are saved in the file /home/averages.txt.
The input and output file pathnames are also parameterized.
Now for the code:
There is nothing bash specific about this script. It will also work with ksh or any other shell that meets basic POSIX shell syntax requirements.
I have a file which is one big long line of text about 10Kb long.
Can someone provide a way using awk to introduce carriage returns every 40 chars in this file.
Any other solutions would also be welcome.
Thank you in advance. (5 Replies)
Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
I have a file with 8 fields. I need the subtotals for fields 7 & 8 when field 5 changes.
cat wk1
01/02/2011/18AB/17/18/000000071/000000033
01/02/2011/18AB/17/18/000000164/000000021
01/02/2011/18AB/17/18/000000109/000000023
01/02/2011/28FB/04/04/000000000/000000000... (2 Replies)
Hi,
Here i have to check first record $3 $4 with second record $1 $2 respectively. If match found, then check first record $2 == second record $4 , if it equals , then reduce two records to single record like as desired output.
Input_file
1 1 2 1
2 1 3 1
3 1 4 1
3 1 3 2
desired... (3 Replies)
Hi,
how do i read a file using awk for a given no of line?
e.g
1. read only first 50 line.
2. read starting from line 20 to line 60..
thanks in advance.
-alva (8 Replies)
I want to count lines of a file using AWK (only) and not in the END part like this awk 'END{print FNR}' because I want to use it.
Does anyone know of a way?
Thanks a lot. (7 Replies)
Sorry for the weird title but i have the following problem.
We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
The output of an awk script is the below file. Line 1,3 that starts with the Ion... need to be under line 2,4 that starts with R_. The awk runs but no output results. Thank you :).
file
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV40... (6 Replies)
The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :).
f1
... (2 Replies)