对齐列中的命令输出

对齐列中的命令输出

我的脚本有以下输出:

panos@panos:~/scripts> ./list_packages openSUSE-2016-254
zypper-aptitude.noarch : 1.12.23-1.1 update needed
zypper-log.noarch : 1.12.23-1.1 update needed
libsolv-debugsource : None not installed
libsolv-demo : None not installed
libsolv-demo-debuginfo : None not installed
libsolv-devel : None not installed
libsolv-devel-debuginfo : None not installed
libsolv-tools : 0.6.14-1.1 update needed
libsolv-tools-debuginfo : None not installed
perl-solv : None not installed
perl-solv-debuginfo : None not installed
python-solv : 0.6.14-1.1 update needed
python-solv-debuginfo : None not installed
ruby-solv : None not installed
ruby-solv-debuginfo : None not installed
libzypp : 15.19.5-1.1 update needed
libzypp-debuginfo : None not installed
libzypp-debugsource : None not installed
libzypp-devel : None not installed
libzypp-devel-doc : None not installed
zypper : 1.12.23-1.1 update needed
zypper-debuginfo : None not installed
zypper-debugsource : None not installed

输出是根据一些 if-else 语句生成的。让我给你我的源代码中使用的三个 echo 命令:

echo "$pkg : $pkg_version update needed"
echo "$pkg : $new_version updated"
echo "$pkg : None not installed"

我的问题是希望它们位于列中,例如:

$pkg\t$pkg_version\t$message

但由于某些包名的长度超过 8 个字符,整个“制表符”变得很难看。有什么建议么?

答案1

有两种方法:

  1. 使用 bash'printf函数打印并格式化输出(而不是echo
  2. 使用column -s : -t命令

./list_packages openSUSE-2016-254 | column -s : -t 
zypper-aptitude.noarch     1.12.23-1.1 update needed
zypper-log.noarch          1.12.23-1.1 update needed
libsolv-debugsource        None not installed
libsolv-demo               None not installed
libsolv-demo-debuginfo     None not installed
libsolv-devel              None not installed
libsolv-devel-debuginfo    None not installed
libsolv-tools              0.6.14-1.1 update needed
libsolv-tools-debuginfo    None not installed
perl-solv                  None not installed
perl-solv-debuginfo        None not installed
python-solv                0.6.14-1.1 update needed
python-solv-debuginfo      None not installed
ruby-solv                  None not installed
ruby-solv-debuginfo        None not installed
libzypp                    15.19.5-1.1 update needed
libzypp-debuginfo          None not installed
libzypp-debugsource        None not installed
libzypp-devel              None not installed
libzypp-devel-doc          None not installed
zypper                     1.12.23-1.1 update needed
zypper-debuginfo           None not installed
zypper-debugsource         None not installed

答案2

您可以使用类似的东西:while read -r arg1 arg2; do awk '{print "package: $arg1 version: $arg1}';done < input file

所以想法是 arg1 将始终是该行中的第一个单词,arg2 将是第二个单词,依此类推。您可以使用if grepetc 来实现所需的目标。希望有帮助!

相关内容