[英]Bash print columns with varying number of values
我正在尝试创建一个报告,从原始源文件分解不同的依赖文件,然后将其输出到带有列的报告中。 到目前为止,我可以生成文件列表,但第四列是唯一具有多个值的列,这就是我得到的:
file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'
paste <(printf %s "$file |") <(printf %s "$fs_file |") <(printf %s "$gen_file |") <(printf "%10s\n""$item_file")
输出:
foo.rcf | foo3.rcf | foo2.rcf | foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf
我希望能够输出的是以下内容:
foo.rcf | foo3.rcf | foo2.rcf | foo4.rcf
| | | foo5.rcf
| | | foo6.rcf
| | | foo7.rcf
bash 解决方案会很棒,但此时我真的在寻找任何解决方案。
使用列。 首先使用paste
连接使用一些分隔符的行,然后使用由该分隔符分隔的字段对输出进行column
化:
file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'
paste -d'|' <(
printf %s "$file") <(
printf %s "$fs_file") <(
printf %s "$gen_file") <(
printf %s "$item_file") |
column -t -s '|' -o ' | '
会输出:
foo.rcf | foo3.rcf | foo2.rcf | foo4.rcf
| | | foo5.rcf
| | | foo6.rcf
| | | foo7.rcf
您可以使用 awk:
file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'
# Lets concatenate the string with separators:
printf "$file|$fs_file|$gen_file|$item_file" |
# Sending modified string to awk with pipe---^
# awk can split it with -F parameter and insert tabs between the chunks.
awk -F'|' 'NR==1 {
print $1"\t\t|\t"$2"\t|\t"$3"\t|\t"$4
} NR>1 {
print "\t\t|\t\t\t|\t\t\t|\t"$1 }'
# -F defines a separator, NR is line number.
输出:
foo.rcf | foo3.rcf | foo2.rcf | foo4.rcf
| | | foo5.rcf
| | | foo6.rcf
| | | foo7.rcf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.