Shell 脚本输出格式化 echo

问题描述 投票:0回答:4

我有如下 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

是否有任何命令可以完成此操作。我用的是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

谢谢

bash unix format echo
4个回答
1
投票

使用制表符分隔列,然后使用标准

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
选项指定的制表位足够大,可以容纳每列中最长的字符串。


1
投票

假设您可以更改脚本,一种想法是将

echo
调用替换为
printf
,例如:

设置:

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

0
投票

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

改变:

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

致:

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

0
投票

您还可以使用

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
完成了魔法。

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

© www.soinside.com 2019 - 2024. All rights reserved.