0

我正在使用 bash 脚本解析 csv 文件,我的输出将采用包含行数和列数的表格形式,因此当我将输出重定向到文本文件对齐不匹配并且看起来很乱时。

任何人都可以指导我如何将我的输出重定向到 html 格式或用任何其他替代方式建议我。

提前致谢

4

3 回答 3

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
于 2012-06-01T15:49:26.883 回答
0

一个简单的解决方案将使用 column(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
于 2012-06-02T12:26:21.020 回答
0

将它写在 TSV 中,然后让 XSLT 样式表将其从 TSV 转换为 XHTML。您可以$'\t'在 bash 中使用来生成制表符。

于 2012-06-01T15:08:32.830 回答