问题
OK I have always had this problem. I want JUST the available updates listed in a file via bash script from a Linux system (RHEL or Fedora) using yum but I always have to deal with the Header information created which looks like this:
Loaded plugins: XXXX-repo XXXX-updates
: WWWWWW-repo something-updates QQQQQ-updates
Updated packages
package1.i686 1:234 RHEL 6.5 updates
package2.i686 1:234 RHEL 6.5 updates
package3.i686 1-234 RHEL 6.5 updates
package4.noarch 1.234 RHEL 6.5 updates
All I want is a list of package1,package2, etc. which seems simple enough but it isn't because I can't just grep on "updates" or ":". Am I looking at this wrongly? Why would I not want to capture what updates were found in a script? Should I just update and check what has been updated instead? Thoughts?
PS> I can not use --noplugins option.
EDIT: So far I have come up with this,
sudo yum check-update | grep "\." | awk '(NR >=1) {print $1;}' | grep '^[[:alpha:]]'
Basically grab the lines with a period in them, the first line, and make sure it first contains alpha letters. Perhaps over done but it seems to work.
回答1:
To only print lines following (but not including) "Updated packages"
yum check-update | awk 'p; /Updated packages/ {p=1}'
Note, on my Fedora system, a blank line separates the "header" from the list of updatable packages, so I would use awk 'p;/^$/{p=1}'
回答2:
If you pipe the output above into awk using this command:
| awk '(NR >=4) {print $1;}'
You will get the following output
package1.i686
package2.i686
package3.i686
package4.noarch
The (NR >=4) tells awk to ignore the first three lines. The {print $1;} tells awk to print the first word of each line.
You can read here for more information on cutting stuff out after certain characters on each line. You can then use sed if stripping out everything after the . is important
| awk '(NR >=4) {print $1;}' | sed s/\.[^\.]*$//
Gives the following output
package1
package2
package3
package4
Then pipe it into another sed command to replace the linebreaks with a comma.
| awk '(NR >=4) {print $1;}' | sed s/\.[^\.]*$// | sed ':a;N;$!ba;s/\n/,/g'
Yields the following output
package1,package2,package3,package4
回答3:
A more flexible solution
The solution below does not assume a specific number of lines in the Header (Ex. in CentOS I got much more header lines).
Nor does it suppose that you are only interested in the repository updates
.
yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }' > updates
Example
For the following yum check-update
output
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 31 kB 00:00:00
* base: asi-fs-m.net
Excluding mirror: mirror.de.leaseweb.net
Excluding mirror: mirror.fra10.de.leaseweb.net
base | 3.6 kB 00:00:00
cwp | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
mariadb | 2.9 kB 00:00:00
remi-safe | 3.0 kB 00:00:00
updates | 3.4 kB 00:00:00
remi-safe/primary_db | 1.4 MB 00:00:00
openvpn.x86_64 2.4.7-1.el7 epel
polkit.x86_64 0.112-18.el7_6.1 updates
pure-ftpd.x86_64 1.0.47-2.el7 epel
remi-release.noarch 7.6-2.el7.remi remi-safe
You can get
openvpn.x86_64
polkit.x86_64
pure-ftpd.x86_64
remi-release.noarch
Explanation
This solution assumes that the relevant lines have the pattern<package name><spaces><version number><spaces><repo name>
If you want to output a particular repository, then use the pattern/\S+\s+[0-9]\S+\s+repo_name/
PS:
If this solution does not work in your system, let me know in a comment
回答4:
Try this:
yum check-update | awk '{if($5 ~ /updates/){print $1}}' | tr '\n' ','
If the input contains 'updates' on fifth column then print first column and create a csv list.
回答5:
Isn't this easier:
yum check-update -q | awk '{print $1}'
Edited.
The explanation of the command:
yum check-update -p - will list the updates in a 3 column list which are package + new version + repository.
awk '{ print $1 }' - will pick up the first column ( package )
来源:https://stackoverflow.com/questions/23698638/how-to-get-just-a-list-of-yum-updates