3

I have log file which shows ten values and last column value is memory in bytes. If I wanted to display highest memory used in last four days, what command or script should I use? With the help of curl or awk command how do I get the max memory value used in last four days?

The log file's name is ansh.log, and it contains the following:

 Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890
Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987201

Now I want last column (memory) highest value . I have last four days log lines in this log file so it is very big file.

1
  • 2
    Regarding With the help of curl or awk command - how could curl play a part in this?
    – Ed Morton
    Nov 18, 2021 at 2:19

5 Answers 5

5

Since we're only looking for the max value, we don't need to sort, just scan through the file:

awk -F "=" '
    $NF > max {max = $NF}
    END {print max}
' ansh.log

If you love a one-liner

awk -F= '$NF > max {max = $NF} END {print max}' ansh.log
3

You can get it with sed/cut/awk as you prefer to separate it & then sort answers with displaying only the last one then you got the highest one

example with many sub commands to be easier to read:

$
 cat > /tmp/a #define a fake-file for test/demo
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987201
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987555
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64798797
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64911111
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=61111111
$
  sed "s/ Time/\nTime/1 ; s/.*=//" /tmp/a | sort -n | tail -1 # one line for each timestamp even we have many on same line / then get only number / then sort numbers / then keep only the last one (higher cause they are sorted)
64987555 
$
 variable=$(sed "s/ Time/\nTime/1 ; s/.*=//" /tmp/a | sort -n | tail -1)  # if your want that in a variable only
$
 echo ${variable}
64987555
$
 rm /tmp/a  # remove test-file 
3

Since @glennjackman already has the awk script covered, here's an alternative that's actually pretty concise and efficient assuming you always have 4 =s on each line as shown in your sample input:

$ cut -d'=' -f5 file | sort -rn | head -1
64987201

If you have variable numbers of = you could always replace that cut command with awk -F'=' '{print $NF}'.

1

Using Raku (formerly known as Perl_6)

raku -ne 'state @a.push(.match: /<?after memory\= > <digit>+ $/ ); END put @a.max;'

Sample Input:

Timestamp, xyz=1, abc=2, def=6, memory=64357890
Timestamp, xyz=1, abc=2, def=6, memory=64987201

Sample Output:

64987201

Above a solution is coded in Raku, a member of the Perl-family of programming languages.

Briefly, raku is run at the command line with the -ne (linewise, non-autoprinting) flags. An array @a is state-d and immediately has values push-ed upon it. The values push-ed upon it are one-or-more <digit>+-s captured in the match operation. The match operation employs Raku's zero-width positive lookbehind construct <?after ... >. A ? (positive) match is demanded after the escaped string memory\=. The entire lookbehind reads as follows: <?after memory\= >. At the END of linewise operations, the max of values in @a are put (printed-using-terminator).

There are simpler ways to code this solution such as comparing each value to a preceding value and keeping the max in every case. However this solution has the advantage that all memory values are extracted and held in @a for later use, such as for statistical analysis (e.g. mean).

https://raku.org

0

Here's another one similar to Ed Morton answer but this doesn't require a fixed number of =:

grep -oh "memory.[0-9,=]*" file | cut -d'=' -f2 | sort -rn | head -1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .