Sponsored Content
Full Discussion: awk last n lines of file
Top Forums Shell Programming and Scripting awk last n lines of file Post 302906856 by Don Cragun on Tuesday 24th of June 2014 03:38:18 AM
Old 06-24-2014
Quote:
Originally Posted by 1in10
First I beg your pardon for being offline three consecutive days since my provider dropped my line and returned to work just today . That turned me into countless tries about the awk-statement, without a result that is close to my target.
Second, I never said the uptime is linked to any user being logged in or using the computer. Though the given output above shows three users, it is me,myself and I that uses this computer. I always said the uptime just as it is, the computer being switched on and probably connected to the net. The output of time as a sum or an average should be hhh:mm, nothing more. That means for example 123 (hours) : 23 (minutes). Or 123:23. Does this collide with my target to measure the n times before? Your fourth point matters to me, because I said above that my aim is to switch the MAC and perhaps make a redial after a certain time.
Actually now the bash is running your given command and I'll wait to respond to pass you that output as well as my desired output. Meanwhile I will study your source-code. Thanks for your persistence!

---------- Post updated at 08:50 PM ---------- Previous update was at 07:39 PM ----------

So here it is, as I hope the way you expected it. This is my version, setting the date at the end. The last three lines.
Code:
15:52:02 up 4:53, 2 users, load average: 0.00, 0.01, 0.04 23.06.2014
15:54:09 up 4:55, 2 users, load average: 0.67, 0.22, 0.11 23.06.2014
15:54:31 up 4:55, 2 users, load average: 0.48, 0.20, 0.10 23.06.2014

What I want is to sum up the third row, the format should be hhh:mm. After trying this, it stumbled upon double digits in that row and stopped at something like 7:56, but that surely is not the maximum of that row.
Code:
awk 'BEGIN {max = 0} {if ($3>max) max=$3} END {print max}' /logfile.txt

That is what it writes until now, my own script. So I ran your code to see the result and return the output of your last command. As it is the first twelve lines
Code:
19:13:44 up  8:15,  2 users,  load average: 0.12, 0.17, 0.10
 19:14:42 up  8:16,  2 users,  load average: 0.04, 0.14, 0.09
 19:15:40 up  8:17,  2 users,  load average: 0.18, 0.16, 0.10
 19:16:38 up  8:18,  2 users,  load average: 0.15, 0.16, 0.10
 19:17:36 up  8:18,  2 users,  load average: 0.06, 0.13, 0.09
 19:18:34 up  8:19,  2 users,  load average: 0.14, 0.13, 0.09
 19:19:32 up  8:20,  2 users,  load average: 0.10, 0.12, 0.09
 19:20:30 up  8:21,  2 users,  load average: 0.16, 0.13, 0.09
 19:21:28 up  8:22,  2 users,  load average: 0.06, 0.10, 0.09
 19:22:26 up  8:23,  2 users,  load average: 0.52, 0.22, 0.12
 19:23:24 up  8:24,  2 users,  load average: 0.26, 0.21, 0.12
 19:24:22 up  8:25,  2 users,  load average: 0.14, 0.18, 0.12

the last five lines of your given command
Code:
20:32:03 up  9:33,  3 users,  load average: 0.00, 0.04, 0.06
 20:33:01 up  9:34,  3 users,  load average: 0.00, 0.03, 0.06
 20:33:59 up  9:35,  3 users,  load average: 0.03, 0.04, 0.06
 20:34:57 up  9:36,  3 users,  load average: 0.01, 0.03, 0.05
 20:35:55 up  9:37,  3 users,  load average: 0.00, 0.02, 0.04

and the last lines of the update to the uplog.txt file
Code:
15:52:02 up 4:53, 2 users, load average: 0.00, 0.01, 0.04 23.06.2014
15:54:09 up 4:55, 2 users, load average: 0.67, 0.22, 0.11 23.06.2014
15:54:31 up 4:55, 2 users, load average: 0.48, 0.20, 0.10 23.06.2014

Code:
mins[4] = 0 from users,
mins[5] = 0 from users,
mins[6] = 582 from 9:42
Input file /home/sandy/logger/uplog.txt trimmed from 216 lines to 200 lines

What do you mean by the text marked in red above? I thought you wanted to get the sum and average values of the 3rd FIELD (not ROW) from the last 7 and 30 lines??? And, this is the first you have said about wanting a maximum instead of a sum and average???

When I asked you to run uptime for a couple of hours, what we need to see is what happens around multiples of one hour and around an integral multiple of one day of up time. So, instead of skipping over the data between what you showed us:
Code:
 19:22:26 up  8:23,  2 users,  load average: 0.52, 0.22, 0.12
 19:23:24 up  8:24,  2 users,  load average: 0.26, 0.21, 0.12
 19:24:22 up  8:25,  2 users,  load average: 0.14, 0.18, 0.12
... ... ...
20:32:03 up  9:33,  3 users,  load average: 0.00, 0.04, 0.06
 20:33:01 up  9:34,  3 users,  load average: 0.00, 0.03, 0.06
 20:33:59 up  9:35,  3 users,  load average: 0.03, 0.04, 0.06

show us what is on the lines starting with 19:58, 19:59, 20:00, 20:01, and 20:02. And we need to see what happens around the time when the system has been running for an even multiple of 24 hours. (At least on OS X, the format on one hour multiples, is something like 9 hours, 17 secs, (instead of 9:00,) and on 24 hour multiples the change is even more pronounced.)

If you got the output:
Code:
mins[4] = 0 from users,
mins[5] = 0 from users,
mins[6] = 582 from 9:42
Input file /home/sandy/logger/uplog.txt trimmed from 216 lines to 200 lines

from my script, I obviously left in a debugging statement that I forgot about. Note also that the output here says that that the 1st 16 lines from that file were discarded (thereby keeping the last 200 lines) as you requested. Is the last line in your log file:
Code:
uptime sandy an jarbo3   9:42   23.06.2014

in the format you want?

Obviously, the format I used to write the sums and averages in /home/averages.txt (or whatever pathname you changed it to) is not in the format you want. But, as I said before, I made a wild guess at a reasonable output format based on the data being summed and averaged. If you will actually show us the last few lines of your log file AND show us exactly what the corresponding sum and average output should be for those lines, we might be able to produce data in the format you want. Did you even look in this file to see the output my script produced?

PLEASE DON'T TELL ME I WROTE THE WRONG SUMS AND AVERAGES IF YOU REFUSE TO SHOW US WHAT YOU WANT!

As I said before, your command:
Code:
awk 'BEGIN {max = 0} {if ($3>max) max=$3} END {print max}' /logfile.txt

tells you absolutely nothing about the data on the last 7 or on the last 30 lines in your log file; it is looking for the maximum value in all 200 lines in your log file. And, it is doing a string comparison (not a numeric comparison of hours and minutes); so 7:56 is greater than 22:30, but even more importantly $3 seems to be what you want for looking at the output from uptime not the data in your log file where I hope the up time is in field 5:
Code:
uptime sandy an jarbo3   9:42   23.06.2014

shown in red here; not field 3 shown in orange. Or, have you changed the data format in your log file again!
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with splitting lines in a file using awk

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)
Discussion started by: martinbarretto
5 Replies

2. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

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)
Discussion started by: capnino
4 Replies

3. UNIX for Dummies Questions & Answers

How do you subtotal lines in a file? Awk?

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)
Discussion started by: MS75001
2 Replies

4. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

5. Shell Programming and Scripting

Reducing file lines in awk

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)
Discussion started by: vasanth.vadalur
3 Replies

6. Shell Programming and Scripting

Read a file using awk for a given no of lines.

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)
Discussion started by: alvagenesis
8 Replies

7. Shell Programming and Scripting

Counting lines in a file using awk

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)
Discussion started by: guitarist684
7 Replies

8. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

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)
Discussion started by: SDohmen
28 Replies

9. Shell Programming and Scripting

awk to reorder lines in file

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)
Discussion started by: cmccabe
6 Replies

10. UNIX for Beginners Questions & Answers

awk to average matching lines in file

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)
Discussion started by: cmccabe
2 Replies
All times are GMT -4. The time now is 11:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy