Sayan Roy
Sayan Roy

Reputation: 43

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"

I'm running the aforementioned code within a 'for' loop in bash.The variables 'station1','code1','station2','code2' are changing after each iteration of the loop and it gives me an output like this:

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

How to print those lines in such a way that my output looks like this:

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

Upvotes: 1

Views: 170

Answers (1)

Georg P.
Georg P.

Reputation: 3164

First put the variable length string into a temp variable, then use printf to output it in formatted way:

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

Upvotes: 2

Related Questions