以表格格式打印 Shell 脚本
我正在尝试使用 shell script 以表格式打印一组值。该表有 n 行和 4 列。我尝试了下面的代码。
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
上面的代码从各种文件中查找不同的字段,并根据格式说明符进行打印。
但我看到的是输出形式的错位。有没有办法以表格格式打印值?列宽是固定的,如果单元格中的值超过宽度,则它必须自行换行。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
echo
,而是使用printf
命令。您可以使用
%s
表示字符串,使用%d
表示整数:您可以根据屏幕使用空格,使用
%20s
来使用字符串 20 个字符,整数%8d
。格式控制选项也可用:
\n
:换行\t
:制表符(水平)\v
:制表符(垂直)Instead of using
echo
, use theprintf
command.You can use
%s
for a string and%d
for an integer: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)只是明确 printf 中不需要逗号。
要打印的所有内容都应该写在双引号中,在这些引号之后,您必须提及在双引号内使用的变量。
例如:
输出:
问题的确切答案是(假设变量值计算正确):
打印标题:
打印值:
当然,制表符的数量 (\t) 取决于您的数据长度。
Just making clear that comma is not needed in printf.
Everything that is to be printed should be written in double quotes and after these quotes you have to mention variables which are used inside double quotes.
For example:
outputs:
The exact answer to the question is (assuming values for variable are calculated correctly):
Printing heading:
printing values:
Of course number of tabs (\t) depends on your length of data.