如何解决表格向右移动的问题。我只是希望它显示在 下1
。
这是我添加了 START 的脚本。现在对齐已经变得不稳定了:
while IFS="," read f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
do
printf "START %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %5s" $f1 $f2 $f3 $f4 $f5 $f6 $f7 $f8 $f9 $f10 $f11 $f12; # ifet the student id
done < records.csv
echo " Press <enter> to return to main menu"
read null
答案1
您需要在 printf 语句末尾添加换行符,如下所示:
printf "START %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %5s\n" $f1 $f2 $f3 $f4 $f5 $f6 $f7 $f8 $f9 $f10 $f11 $f12; # ifet the student id
(注意\n
)
答案2
这可能是因为,您只为第四个字段提供了 5 个位置,但它可能有六个字符,如HARRY1
您的示例所示:
printf "%10s %10s %10s %5s <...>
^^^
然而,GNU/Linux 中有很好的美化器column
它根据传入数据自动选择列宽:
$ cat test.csv
Harry,2
Ed,3
Mary,4
Looooooooongname,8
$ sed 's/,/ /' test.csv | column -t
Harry 2
Ed 3
Mary 4
Looooooooongname 8
$ sed 's/,/ /' test.csv | grep -v Looooooooongname | column -t
Harry 2
Ed 3
Mary 4
也许这对您来说是更好的选择?