Home > Enterprise >  Extract second word from a file in yum repository file using command line or bash
Extract second word from a file in yum repository file using command line or bash

Time:06-20

I wanted to extract a paticular file path from a text file (yum repository file) The file contents looks like this

[rhel-8-for-x86_64-baseos-rpms]
name = Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)
baseurl = https://myhost.com
enabled = 1
gpgcheck = 1
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
sslverify = 1
sslclientkey = /etc/pki/entitlement/60472949067-key.pem
sslclientcert = /etc/pki/entitlement/604712349067.pem
metadata_expire = 1
enabled_metadata = 1

[rhel-8-for-x86_64-appstream-rpms]
name = Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
baseurl = https://myhost.com
enabled = 1
gpgcheck = 1
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
sslverify = 1
sslclientkey = /etc/pki/entitlement/6047297349067-key.pem
sslclientcert = /etc/pki/entitlement/6047297349067.pem
metadata_expire = 1
enabled_metadata = 1

My requirement is to extract sslclinetkey path of both rhel-8-for-x86_64-appstream-rpm and rhel-8-for-x86_64-baseos-rpm through the command line or script

Any help would be appreciated

UPDATE

I tried this way, but I am getting multiple entries

cat /etc/yum.repos.d/redhat.repo  | grep sslclientkey | awk '{print $3}'
/etc/pki/entitlement/2627848977587647846-key.pem
/etc/pki/entitlement/7675461792121821585-key.pem
/etc/pki/entitlement/3236342435348109549-key.pem
/etc/pki/entitlement/60472974412349067-key.pem
/etc/pki/entitlement/3236342435348109549-key.pem
/etc/pki/entitlement/60472974412349067-key.pem

But I only need of rhel-8-for-x86_64-appstream-rpm and rhel-8-for-x86_64-baseos-rpm

CodePudding user response:

awk -F' = ' '/sslclientcert/{print $2}' file 
# or 
awk '/sslclientcert/{print $3}' file 

/etc/pki/entitlement/604712349067.pem
/etc/pki/entitlement/6047297349067.pem

awk -F' = ' '/\[.*\]/ {printf "%s ", $1} /sslclientcert/{print $2}' file 
[rhel-8-for-x86_64-baseos-rpms] /etc/pki/entitlement/604712349067.pem
[rhel-8-for-x86_64-appstream-rpms] /etc/pki/entitlement/6047297349067.pem
  • Related