输出查询变量

时间:2014-11-26 01:07:05

标签: database bash db2

我有这个:

db2 -x "SELECT title, description FROM pages" | while read title description ; do
    echo "$title $description";
done

然而,当它回声时,它输出如下:

Hello World                                        Description
Another Title                                      Description
Title Description

正如您所看到的,如果标题有2个单词,则会将描述打印到侧面,而如果标题是1个单词,则会在标题旁边打印说明。

如何以格式化和有条理的方式输出结果?

2 个答案:

答案 0 :(得分:0)

db2 -x 'SELECT title, description FROM pages' |
awk '{printf "%-13s  %s\n", $1, $2}' FS=' {2,}'

结果

Hello World    Description
Another Title  Description
Title          Description

答案 1 :(得分:0)

你可以像这样使用'-e'选项:

db2 -x "SELECT title, description FROM pages" | while read title description ; do
    echo -e "$title \t $description";
done

Hello World            Description
Another Title          Description
Title                  Description


-e     enable interpretation of backslash escapes

\t     horizontal tab

了解更多信息Man Page echo