2012-06-01 32 views
0

我解析使用bash脚本的csv文件,我的输出将以表格形式与行数和颜色,所以当我重定向我的输出到文本文件对齐不匹配,看起来如此混乱。我希望我的bash脚本以html格式输出?

任何人都可以指导我如何将我的输出重定向到html格式或建议我与另一种方式。

在此先感谢

+0

为什么这个操作,最后你的目标是什么? –

+0

看到[this](http://stackoverflow.com/questions/2033268/linux-shell-output-to-html)如果它帮助你 –

回答

0

写出来的TSV,然后有一个XSLT样式表转换从TSV到XHTML。您可以在bash中使用$'\t'来生成制表符。

+0

TSV .. XSLT ....对我来说完全是一个非常新的概念。让我试试这个:)谢谢你的建议.. –

2

如果您确实不需要HTML中的输出,但您在使用制表符进行列对齐时遇到问题,则可以使用printf获得良好的列对齐。

顺便说一句,如果你的问题包括一些示例输入,你用来分析和输出它的脚本以及一些示例输出,这将有所帮助。

这里是printf一个简单的例子:

$ cat file 
example text,123,word,23.12 
more text,1004,long sequence of words,1.1 
text,1,a,1000.42 

$ cat script 
#!/bin/bash 
headformat='%-*s%-*s%*s%*s\n' 
format='%-*s%-*s%*d%*.*f\n' 
modwidth=16; descwidth=24; qtywidth=6; pricewidth=10 
printf "$headformat" "$modwidth" Model "$descwidth" Desc. "$qtywidth" Qty "$pricewidth" Price 
while IFS=, read model quantity description price 
do 
    printf "$format" "$modwidth" "$model" "$descwidth" "$description" "$qtywidth" "$quantity" "$pricewidth" 2 "$price" 
done < file 

$ ./script 
Model   Desc.      Qty  Price 
example text word      123  23.12 
more text  long sequence of words 1004  1.10 
text   a       1 1000.42 
0

一个简单的解决沃尔德使用柱(1):

column -t -s, <(echo "head1,head2,head3,head4"; cat csv.dat) 

与像这样的一个结果是:

head1 head2  head3 head4 
aaaa 33333  bbb  123 
aaa 333333 bbbx 123 
aa  3333333 bbbxx 123 
a  33333333 bbbxxx 123