2016-08-17 74 views
0

我有一个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 

回答

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 

I如果你不需要,只需发表评论并让我知道,我很乐意进一步提供帮助。

(你可能需要调整的字段宽度了一下,我只是做了一个粗略一算)


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

如果你想投保您的数据总是以表格的形式显示,您需要测量每个字段宽度(包括标题),然后采用字段宽度(或标题)的最大值来设置输出的字段宽度。下面是如何能(使用模拟其他程序输入)工作的一个示例:

#!/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更好)

输出到日志。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 

看看,让我知道如果您有任何问题。

+0

这是行不通的,因为数组中有一些行更长,更短。所以,用空格分隔它们并不是很精确的列表。 – user3164754

+0

这就是为什么我说你可能需要将* field width *修饰符调整为该字段的最长字符串。您可以将* field width *作为参数提供给每个'printf' *转换说明符*(例如'%* s'并且您传递'printf“%* s ...”“$ width”“$ field”'(您可以使用'$ {#varname}'获得每个字段的最大宽度,例如'$ {#客户端名称} ' –

+0

我尝试了不同宽度的字段,但仍然输出不是很漂亮,我的意思是我能够分开字段,但它们仍然不在一条直线上面只是一个示例,我还有很多这样的线条的长度有所不同 – user3164754

1

我不知道理解万物= P

但要回答这个问题:

我如何制表后的名字---?

echo -e "Example1\tExample2" 

-e意思是:使反斜线的解释转义

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

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

编辑:如果您需要更多的空间,你可以双击,三重... it

echo -e "Example1\t\tExample2" 
+0

对您有帮助吗? –