制表由shell编写的部分文本文件

时间:2016-08-17 17:08:26

标签: shell

我有一个shell脚本,它将数组上的输出写入(回显)到文件中。该文件采用以下格式

The tansaction detials for today are 35 

    Please check the 5 biggest transactions below   

-----------------------------------------------------------------------------------


Client Name,Account Number,Amount,Tran Time
Michael Press,20484,602117,11.41.02
Adam West,164121,50152,11.41.06
John Smith,15113,411700,11.41.07
Leo Anderson,2115116,350056,11.41.07
Wayne Clark,451987,296503,11.41.08

我有多条这样的线。

如何在---后列出名字? 我在回显数组元素时尝试使用空格。还试过标签。我尝试使用column -t -s选项。但是---上面的文字正在干扰所需的输出。

所需的输出是

The tansaction detials for today are 35 

    Please check the 5 biggest transactions below   

-----------------------------------------------------------------------------------


Client Name   Account Number   Amount  Tran Time
Michael Press 20484            602117  11.41.02
Adam West     164121           50152   11.41.06
John Smith    15113            411700  11.41.07
Leo Anderson  2115116          350056  11.41.07
Wayne Clark   451987           296503  11.41.08

打印到文件是更大脚本的一部分。所以,我正在寻找一个插入这个脚本的简单解决方案。 这是来自该脚本的片段,我回复该文件。

echo "The tansaction detials for today are 35 " >> log.txt
echo "" >> log.txt
echo "  Please check the 5 biggest transactios below   " >> log.txt
echo "" >> log.txt
echo "-----------------------------------------------------------------------------------" >> log.txt
echo "" >> log.txt
echo "" >> log.txt
echo "Client Name,Account Number,Amount,Tran Time" >> log.txt
array=( `output from a different script` )
x=1
for i in ${array[@]}
do

        #echo "Array $x - $i"
        Clientname=$(echo $i | cut -f1 -d',')
        accountno=$(echo $i | cut -f2 -d',')
        amount=$(echo $i | cut -f3 -d',')
        trantime=$(echo $i | cut -f4 -d',')

echo "$Clientname,$accountno,$amount,$trantime" >> log.txt
(( x=$x+1 ))
done

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,为了产生输出格式:

Client Name   Account Number   Amount  Tran Time
Michael Press 20484            602117  11.41.02
Adam West     164121           50152   11.41.06
John Smith    15113            411700  11.41.07
Leo Anderson  2115116          350056  11.41.07
Wayne Clark   451987           296503  11.41.08

您应该使用printf提供的输出格式而不是echo。例如,对于标题,您可以使用:

printf "Client Name   Account Number   Amount  Tran Time\n" >> log.txt

而不是:

echo "Client Name,Account Number,Amount,Tran Time" >> log.txt

编写五个最大金额和详细信息时,您可以使用:

printf "%-14s%-17s%8s%s\n" "$Clientname" "$accountno" "$amount" "$trantime" >> log.txt

而不是:

echo "$Clientname,$accountno,$amount,$trantime" >> log.txt

如果这不是你需要的,只需发表评论并告诉我,我很乐意进一步提供帮助。

(您可能需要稍微调整字段宽度,我只是做了粗略计算)

真正的表格输出需要测量每个字段

如果要确保数据始终采用表格形式,则需要测量每个字段宽度(包括标题),然后取字段宽度(或标题)的最大值来设置字段宽度。输出。下面是一个如何完成的示例(使用模拟的其他程序输入):

#!/bin/bash

ofn="log.txt"   # set output filename

# declare variables as array and integer types
declare -a line_arr hdg name acct amt trn tmp
declare -i nmx=0 acmx=0 ammx=0 tmx=0

# set heading array (so you can measure lengths)
hdg=( "Client Name"
      "Account Number"
      "Ammount"
      "Tran Time" )

## set the initial max based on headings
nmx="${#hdg[0]}"    # max name width
acmx="${#hdg[1]}"   # max account width
ammx="${#hdg[2]}"   # max ammount width
tmx="${#hdg[3]}"    # max tran width

{ IFS=$'\n'         # your array=( `output from a different script` )
line_arr=($(
cat << EOF
Michael Press,20484,602117,11.41.02
Adam West,164121,50152,11.41.06
John Smith,15113,411700,11.41.07
Leo Anderson,2115116,350056,11.41.07
Wayne Clark,451987,296503,11.41.08
EOF
)
)
}

# write heading to file
cat << EOF > "$ofn"
The tansaction detials for today are 35

    Please check the 5 biggest transactions below

-----------------------------------------------------------------------------------


EOF

# read line array into tmp, compare to max field widths
{ IFS=$','
for i in "${line_arr[@]}"; do
    tmp=( $(printf "%s" "$i") )
    ((${#tmp[0]} > nmx )) && nmx=${#tmp[0]}
    ((${#tmp[1]} > acmx )) && acmx=${#tmp[1]}
    ((${#tmp[2]} > ammx )) && ammx=${#tmp[2]}
    ((${#tmp[3]} > tmx )) && tmx=${#tmp[3]}
    name+=( "${tmp[0]}" )   # fill name array
    acct+=( "${tmp[1]}" )   # fill account num array
    amt+=( "${tmp[2]}" )    # fill amount array
    trn+=( "${tmp[3]}" )    # fill tran array
done
}

printf "%-*s  %-*s  %-*s  %s\n" "$nmx" "${hdg[0]}" "$acmx" "${hdg[1]}" \
"$ammx" "${hdg[2]}" "${hdg[3]}" >> "$ofn"
for ((i = 0; i < ${#name[@]}; i++)); do
    printf "%-*s  %-*s  %-*s  %s\n" "$nmx" "${name[i]}" "$acmx" "${acct[i]}" \
"$ammx" "${amt[i]}" "${trn[i]}" >> "$ofn"
done

(如果您只想在它们之间只有一个space,那么您可以删除最后两个printf语句中每个字段之间的额外space - 对我来说看起来更好2)< / p>

输出到log.txt

$ cat log.txt
The tansaction detials for today are 35

    Please check the 5 biggest transactions below

-----------------------------------------------------------------------------------


Client Name    Account Number  Ammount  Tran Time
Michael Press  20484           602117   11.41.02
Adam West      164121          50152    11.41.06
John Smith     15113           411700   11.41.07
Leo Anderson   2115116         350056   11.41.07
Wayne Clark    451987          296503   11.41.08

仔细看看,如果您有任何问题,请告诉我。

答案 1 :(得分:1)

我不确定每个人都知道= P

但要回答这个问题:

  

如何在---?

之后列出名称
echo -e "Example1\tExample2"

-e表示:启用反斜杠转义的解释

因此,对于您的输出,我建议:

echo -e "$Clientname\t$accountno\t$amount\t$trantime" >> log.txt

编辑:如果您需要更多空间,可以加倍,三倍......它

echo -e "Example1\t\tExample2"