I am trying to print set of values in table format using shell script . The table has n rows and 4 columns. I tried the following piece of code.
btfield=`grep -i -r ^"diag.* <string>" *.txt |awk '{print $5}'|cut -d+ -f 1 |sort -u`
ipaddr=''
i=0
format="%10s\t%10s\t%10s \n"
echo "| Function " " | IP |" " occurences |"
for a in $btfield
do
b=`grep -i -r ^"diag.* <string>" *.txt |grep -i $a |cut -d: -f 1|cut -d_ -f 2|cut -d't' -f 1`
noOcc=`grep -i -r ^"diag.* backtrace" *.txt |grep -i $a|wc -l`
#echo $b
ipaddr[i]=${b}
printf "$format" $a ${ipaddr[i]} $noOcc
i=$((i+1))
#echo $i
done
The above piece of code finds distinct fields from various files and prints according to the format specifier.
But what I am seeing is misaligned form of output. Is there any way to print the values in table format ? The column width is fixed and if the value in cell exceeds the width it must wrap itself.
Sample output:
| reason | STB IP | occurences |
printf 142.25.1.100. 142.25.1.100.
142.25.1.102. 142.25.1.105. 192.168.1.100.
192.168.1.100. 192.168.1.100. 192.168.1.100.
192.168.1.106. 9
class_consol 142.25.1.105. 192.168.1.103.
2
getChar 182.168.1.102. 1
maindat 142.25.1.103. 1
_XN52getappdatafrom3EZN2232_iPjj 142.25.1.103. 142.25.1.103.
182.168.1.106.
Instead of using echo
, use the printf
command.
You can use %s
for a string and %d
for an integer:
printf "%20s %8d %s\n" "$string1" $int1 "$unbounded_string"
You can space according to your screen, by using %20s
for using 20 characters for the string, %8d
for the integer.
Format control options are also available:
\n
: newline
\t
: tab (horizontal)
\v
: tab (vertical)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With