簡體   English   中英

Bash 中的 Output 格式

[英]Output formatting in Bash

echo "Station pairs are $station1($code1) and $station2($code2) | distance=$c degrees or `echo $c|awk '{print $1*111.2}'` km"

我在 bash 的“for”循環中運行上述代碼。變量“station1”、“code1”、“station2”、“code2”在每次循環迭代后都會發生變化,它給了我一個 output,如下所示:

Station pairs are Zhozho2(WT15) and Guoban(WT19) | distance=1.4755108271 degrees or 164.077 km
Station pairs are Zhozho2(WT15) and Yarla(WT20) | distance=1.63208637417 degrees or 181.488 km
Station pairs are Zhozho2(WT15) and Zhungba(ZNBA) | distance=1.28887286927 degrees or 143.323 km
Station pairs are Hill(WT16) and N-Napuk(WT17) | distance=1.22929482902 degrees or 136.698 km
Station pairs are Hill(WT16) and Zhungba(ZNBA) | distance=1.87133807827 degrees or 208.093 km
Station pairs are N-Napuk(WT17) and Barkar(WT18) | distance=1.73933270787 degrees or 193.414 km

如何打印這些行,使我的 output 看起來像這樣:

Station pairs are Zhozho2(WT15)  and Guoban(WT19)  | distance=1.4755108271   degrees or 164.077 km
Station pairs are Zhozho2(WT15)  and Yarla(WT20)   | distance=1.63208637417  degrees or 181.488 km
Station pairs are Zhozho2(WT15)  and Zhungba(ZNBA) | distance=1.28887286927  degrees or 143.323 km
Station pairs are Hill(WT16)     and N-Napuk(WT17) | distance=1.22929482902  degrees or 136.698 km
Station pairs are Hill(WT16)     and Zhungba(ZNBA) | distance=1.87133807827  degrees or 208.093 km
Station pairs are N-Napuk(WT17)  and Barkar(WT18)  | distance=1.73933270787  degrees or 193.414 km

首先將變長字符串放入一個temp變量中,然后使用printf到 output 格式化:

temp=$station1($code1)
printf 'Station pairs are %-15s and ...\n' $temp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM