Reputation: 131
I'm writing a script to grab the last update/patch date on a few hundred servers. Lacking another tool due to various reasons, I've decided to write a script. At the moment I'm using the following command:
sudo yum history | grep [0-9] | grep -E 'Update|Install|U|I' | awk 'NR==1'
Which gives me the first line with an action on it. But it only gives me the first line, I toyed with the idea of grabbing the first 5 rows but that may not be applicable to every situation.
sudo yum history | grep [0-9] | grep -E 'Update|Install|U|I' | awk 'NR>=1&&NR<=5'
So I would like to check the last column or two and if more than x packages have been updated or installed then to grab that row.
Generically speaking the output of yum history is:
18 | first last <username> | 2018-08-30 19:41 | E, I, U | 43 ss
17 | first last <username> | 2018-07-10 15:28 | E, I, U | 230 EE
16 | first last <username> | 2018-04-25 20:08 | E, I, U | 44 ss
15 | first last <username> | 2018-01-30 20:57 | E, I, O, U | 108 EE
14 | first last <username> | 2018-01-30 20:39 | Install | 4
The issue I'm running into is the last two columns can differ in their column position and the last column may just be numeric or it may contain letters or special characters. I want to ignore any last column that has any character that is not numeric, and to evaluate whether the last column has more than 20 packages installed or updated. If the last column is more than 20 packages to then grab that row and only that row.
Upvotes: 0
Views: 167
Reputation: 13998
use awk:
sudo yum history | awk -F ' *\\| *' '$4 ~ /\<(Install|Update|U|I)\>/ && $5 > 20'
Upvotes: 0
Reputation: 561
Use a regular expression, matching for the number in the last column. To print all history records with >=20 alterations:
sudo yum history | \
perl -ne '/\| *(\d+)[^\|]*$/ and $1>=20 and print($_)'
Of - if you only want the time stamp from matching history records:
sudo yum history | \
perl -ne '
@col=split(/\|/);
$col[4]=~/^ *(\d+)/ and $1>=20 and print($col[2],"\n")'
Upvotes: 1