unix Shell脚本输出格式回显

pkln4tw6  于 7个月前  发布在  Unix
关注(0)|答案(4)|浏览(111)

我有一个shell脚本输出如下

##########################################################
Creation date: 15-03-2022 / 15:03
performed by: user
Environment: E_MTE_04
Reference - Repository: TEST
-BSL Number of documents: XXX
-Syr Number of documents: XXX
-DEB Number of documents: XXX
Candidate - Repository: TEST
-BSL Number of documents: XXX
-Syr Number of documents: XXX
-DEB Number of documents: XXX

字符串
我尝试在脚本中使用等间距来格式化它,但输出仍然没有统一的间距。我想像下面这样打印输出。

##########################################################
Creation date:                     15-03-2022 / 15:03
performed by:                      user
Environment:                       E_MTE_04
Reference - Repository:            TEST
-BSL Number of documents:          XXX
-Syr Number of documents:          XXX
-DEB Number of documents:          XXX
Candidate - Repository:            TEST
-BSL Number of documents:          XXX
-Syr Number of documents:          XXX
-DEB Number of documents:          XXX


有没有任何commnads可以做到这一点。我使用bash。这里是代码。

echo "##########################################################" >> $log
echo Creation date: $today" / $time">> $log
echo performed by: $USER >> $log
echo Environment: $firstEnv >> $log
echo Reference - Repository: $firstParamomsID >> $log
echo -BSL Number of documents: XXX >> $log
echo -Syr Number of documents: XXX >> $log
echo -DEB Number of documents: XXX >> $log
echo Candidate - Repository: $secondParamomsID >> $log
echo -BSL Number of documents: XXX >> $log
echo -Syr Number of documents: XXX >> $log
echo -DEB Number of documents: XXX >> $log


谢谢

yquaqz18

yquaqz181#

使用制表符分隔列,然后使用标准的expand实用程序使用空格展开制表符。例如,

printf '%s\t%s\n' "Creation date:" "15-03-2022 / 15:03" \
                  "performed by: " "user" \
                  "Environment: " "E_MTE_04" |
  expand -t 20

字符串
产生

Creation date:      15-03-2022 / 15:03
performed by:       user
Environment:        E_MTE_04


您唯一需要做的是确保-t选项指定的制表位足够大,以容纳每列中最长的字符串。

xienkqul

xienkqul2#

假设您可以更改脚本,一个想法是将printf调用替换为echo,例如:
安装程序:

today='15-03-2022'
time='15:03'
USER=user
firstEnv='E_MTE_04'
firstParamomsID='TEST'
secondaryParamomsID='TEST'

字符串
我们的printf字符串:

fmt="%-30s %s\n"


其中:

  • %-30%s-打印宽度为30的左对齐字段中的第一个参数
  • %s\n-打印第二个参数,后跟一个换行符

替换当前的echo代码如下所示:

echo "##########################################################"
printf "${fmt}" "Creation date:" "${today} / ${time}"
printf "${fmt}" "performed by:" "${USER}"
printf "${fmt}" "Environment:" "${firstEnv}"
printf "${fmt}" "Reference - Repository:" "${firstParamomsID}"
printf "${fmt}" "-BSL Number of documents:" "XXX"
printf "${fmt}" "-5yr Number of documents:" "XXX"
printf "${fmt}" "-DEB Number of documents:" "XXX"
printf "${fmt}" "Candidate - Repository:" "${secondaryParamomsID}"
printf "${fmt}" "-BSL Number of documents:" "XXX"
printf "${fmt}" "-5yr Number of documents:" "XXX"
printf "${fmt}" "-DEB Number of documents:" "XXX"


如果所有的printf调用都在连续的行上(即行之间没有其他代码),你可以使用一个printf来处理所有的字符串; printf将重新应用$fmt,直到它耗尽输入字符串列表,例如:

echo "##########################################################"
printf "${fmt}" "Creation date:"            "${today} / ${time}" \
                "performed by:"             "${USER}" \
                "Environment:"              "${firstEnv}" \
                "Reference - Repository:"   "${firstParamomsID}" \
                "-BSL Number of documents:" "XXX" \
                "-5yr Number of documents:" "XXX" \
                "-DEB Number of documents:" "XXX" \
                "Candidate - Repository:"   "${secondaryParamomsID}" \
                "-BSL Number of documents:" "XXX" \
                "-5yr Number of documents:" "XXX" \
                "-DEB Number of documents:" "XXX"

**注意:**额外的空格(行首,参数之间)是可选的;我添加了额外的空格以提高可读性

这两种方法都产生:

##########################################################
Creation date:                 15-03-2022 / 15:03
performed by:                  user
Environment:                   E_MTE_04
Reference - Repository:        TEST
-BSL Number of documents:      XXX
-5yr Number of documents:      XXX
-DEB Number of documents:      XXX
Candidate - Repository:        TEST
-BSL Number of documents:      XXX
-5yr Number of documents:      XXX
-DEB Number of documents:      XXX

9avjhtql

9avjhtql3#

在标签和变量之间添加空格
变更:

echo Creation date: $today" / $time">> $log

字符串
收件人:

echo Creation date:                     $today" / $time">> $log

quhf5bfb

quhf5bfb4#

您还可以使用column实用程序,如下所示:

#!/usr/bin/bash

# A few variables, just so that I can view what's going on here:
log=/tmp/toto
today=$(date -I)
time=$(date +%H:%M)
USER="user"
firstEnv="E_MTE_04"
firstParamomsID=TEST
secondParamomsID="TEST"

# First decoration line:
echo "##########################################################" >> ${log}

# Start with a temporary variable:
tmp=""

# Build that variable by appending strings, with a newline (\n) 
# every time; the trick is to insert a special character where you
# want column separators; in this example I chose "|":
tmp+="\nCreation date:|${today} / ${time}"

# Another trick is to add a few extra spaces before the column on 
# one line, so that the output will be more widely distributed;
# this can be done on any line, I just made it on the following one:
tmp+="\nperformed by:                    |${USER}"

# Keep on building the ${tmp} string:
tmp+="\nEnvironment:|$firstEnv"
tmp+="\nReference - Repository:|${firstParamomsID}"
tmp+="\n-BSL Number of documents:|XXX"
tmp+="\n-Syr Number of documents:|XXX"
tmp+="\n-DEB Number of documents:|XXX"
tmp+="\nCandidate - Repository:|${secondParamomsID}"
tmp+="\n-BSL Number of documents:|XXX"
tmp+="\n-Syr Number of documents:|XXX"
tmp+="\n-DEB Number of documents:|XXX"

# Now that the ${tmp} string is built, let's process it:
echo -e "${tmp}" | column -s "|" -t >> ${log}

字符串
魔术是由column在最后一行完成的。
这是我得到的输出:

# pierre@latitude: ~ < 2023_11_04__17_17_27 > [bashpid_24830 19]
tail -12 /tmp/toto 
##########################################################
Creation date:                     2023-11-04 / 17:17
performed by:                      user
Environment:                       E_MTE_04
Reference - Repository:            TEST
-BSL Number of documents:          XXX
-Syr Number of documents:          XXX
-DEB Number of documents:          XXX
Candidate - Repository:            TEST
-BSL Number of documents:          XXX
-Syr Number of documents:          XXX
-DEB Number of documents:          XXX


请注意,我系统地用{}包围变量;这是一个很好的做法,一些同事说。

相关问题