以Bash打印表格

问题描述 投票:-1回答:1

我在使用printf函数或echo来打印数组中的所有值以打印漂亮的表时遇到问题。我尝试了简单的时间,但是没有按照我想要的顺序进行。


    for i in ${!R[@]}

    do

      printf "%-20s" ${R[i]}

      printf "\n"

    done

what i havewhat i want

我想要的

 mteEventComment mteEventActions mteEventEnabled mteEventEntryStatus
                           "80 "            true              active
                           "80 "            true              active
                           "80 "            true              active
                           "80 "            true              active
                           "80 "            true              active
                           "80 "            true              active

我有什么

 mteEventComment mteEventActions mteEventEnabled mteEventEntryStatus
       "80          "        "80          "        "80
         "        "80          "        "80          "
       "80          "       true       true       true
      true       true       true     active     active
    active     active     active     active
script2.sh: line 64: "80: syntax error: operand expected (error token is ""80")



 content of **R array**: First 6 are empty
         "80 " "80 " "80 " "80 " "80 " "80 " true true true true true true active active active active active active

我有标题

printf "$header" "mteEventComment" "mteEventActions" "mteEventEnabled" "mteEventEntryStatus"


mteEventComment mteEventActions mteEventEnabled mteEventEntryStatus

在此之下,我需要我的数组以确切的顺序

bash printf echo
1个回答
0
投票

鉴于R中的数据包含带有特殊字符(引号,空格)的值(和可能的键),您想用引号引起它们:

for i in "${!R[@]}" 
    do
      printf "%-20s\n" "${R[i]}"
    done
© www.soinside.com 2019 - 2024. All rights reserved.