我有一个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
这是行不通的,因为数组中有一些行更长,更短。所以,用空格分隔它们并不是很精确的列表。 – user3164754
这就是为什么我说你可能需要将* field width *修饰符调整为该字段的最长字符串。您可以将* field width *作为参数提供给每个'printf' *转换说明符*(例如'%* s'并且您传递'printf“%* s ...”“$ width”“$ field”'(您可以使用'$ {#varname}'获得每个字段的最大宽度,例如'$ {#客户端名称} ' –
我尝试了不同宽度的字段,但仍然输出不是很漂亮,我的意思是我能够分开字段,但它们仍然不在一条直线上面只是一个示例,我还有很多这样的线条的长度有所不同 – user3164754