How can we limit and give space in after each special character in bash script output?

I have to send all rpm related information to our log analyzer and I am running the command below which give continuous output as follows:

CMD

sudo rpm -qa --info | egrep 'Name|Version|Architecture|Install Date|Vendor|Description'

O/P

Name        : openssh-clients
Version     : 7.4p1
Architecture: x86_64
Install Date: Fri 29 Nov 2019 02:31:40 PM UTC
Vendor      : Amazon Linux
Description :
Name        : p11-kit-trust
Version     : 0.23.5
Architecture: x86_64
Install Date: Tue 18 Jun 2019 10:23:44 PM UTC
Vendor      : Amazon Linux
Description :
Name        : ec2-hibinit-agent
Version     : 1.0.0
Architecture: noarch
Install Date: Fri 29 Nov 2019 02:31:44 PM UTC
Vendor      : Amazon Linux
Description :
Name        : python-slip-dbus
Version     : 0.4.0
Architecture: noarch
Install Date: Tue 04 Feb 2020 12:41:09 PM UTC
Vendor      : Amazon Linux
Description :

I would like to split the above output whenever Name comes. It should be split with a space.


Solution 1:

You can format your rpm output using --qf.

Query formats are modified versions of the standard printf(3) formatting. The format is made up of static strings (which may include standard C character escapes for newlines, tabs, and other special characters) and printf(3) type formatters. As rpm already knows the type to print, the type specifier must be omitted however, and replaced by the name of the header tag to be printed, enclosed by {} characters.

I took the egrep out of your statement and used the formatting.

sudo rpm -qa --qf "Name\t    : %{Name}\nVersion\t    : %{Version}\nArchitecture: %{Arch}\nInstall Date: %{INSTALLTIME:date}\nVendor\t    : %{Vendor}\nDescription : %{Description}\n\n"

Output

Name : vim-enhanced
Version : 7.4.629
Architecture: x86_64
Install Date: Tue 29 Oct 2019 02:53:20 PM UTC
Vendor : CentOS
Description : VIM (VIsual editor iMproved) is an updated and improved version of the vi editor. Vi was the first real screen-based editor for UNIX, and is still very popular. VIM improves on vi by adding new features: multiple windows, multi-level undo, block highlighting and more. The vim-enhanced package contains a version of VIM with extra, recently introduced features like Python and Perl interpreters.

Install the vim-enhanced package if you'd like to use a version of the VIM editor which includes recently added enhancements like interpreters for the Python and Perl scripting languages. You'll also need to install the vim-common package.

Name : wget
Version : 1.14
Architecture: x86_64
Install Date: Tue 29 Oct 2019 02:54:15 PM UTC
Vendor : CentOS
Description : GNU Wget is a file retrieval utility which can use either the HTTP or
FTP protocols. Wget features include the ability to work in the background while you are logged out, recursive retrieval of directories, file name wildcard matching, remote file timestamp storage and comparison, use of Rest with FTP servers and Range with HTTP servers to retrieve files over slow or unstable connections, support for Proxy servers, and configurability.

Note: the formatting looks different in markup than it does on my screen.

Also note the egrep wasn't giving you a description because descriptions start on a new line.

And finally, descriptions can have spaces. This method will capture the full description.

You can see what tags are available using:

rpm --querytags

References

  • rpm Man