在 sh 中格式化合并输出

Formatting consolidated output in sh

我目前在 bash 中有一个输出,看起来像:

"a" "a" "b" => no
"a" "a" => yes
"b" => yes
"a" "b" "a" "b" => no

我正在使用 printf,但问题是,为了创建这些字符串,我循环遍历了程序中的一堆结果并将它们连接起来,因此格式无法按我想要的方式工作。我希望它们是两列 - 字母,然后是“=> yes”或“=> no”作为第二列。

这是我设置它们的方式(这都是在一个循环中遍历一堆结果):

while read -r line
do
  python3 ./fsa_acceptor.py "" "$line" >"fsa$counter"
  chmod +x "fsa$counter"
  carmel_result=$(carmel "" "fsa$counter")
  if test -z "$carmel_result"
  then
    output+="$line => no"
  else
    output+="$line => yes"
  fi
  output+="\n"
  counter=$((counter+1))
done < "$filename"
  printf "$output" > result_file 

打印时如何重新格式化?

一个简单的方法是使用列:

$ column -ts "=" result_file 
"a" "a" "b"       > no
"a" "a"           > yes
"b"               > yes
"a" "b" "a" "b"   > no

但是,如您所见,它消耗了等号。如果您的专栏有 -o 标志,请将 -o "=" 添加到命令中,您就完成了。如果没有,您可以使用 Sed 恢复它:

$ column -ts "=" result_file | sed 's/>/=>/'
"a" "a" "b"       => no
"a" "a"           => yes
"b"               => yes
"a" "b" "a" "b"   => no