Bash 将两个命令的输出连接成一行

Bash 将两个命令的输出连接成一行,bash,shell,concatenation,Bash,Shell,Concatenation,我这里有一个非常基本的shell脚本: for file in Alt_moabit Book_arrival Door_flowers Leaving_laptop do for qp in 10 12 15 19 22 25 32 39 45 60 do for i in 0 1 do echo "$file\t$qp\t$i" >> psnr.txt ./command > $

我这里有一个非常基本的shell脚本:

for file in Alt_moabit Book_arrival Door_flowers Leaving_laptop
do
    for qp in 10 12 15 19 22 25 32 39 45 60
    do
        for i in 0 1
        do
            echo "$file\t$qp\t$i" >> psnr.txt
            ./command > $file-$qp-psnr.txt 2>> psnr.txt
        done
    done
done
命令
计算一些PSNR值,并将
文件
qp
i
的每个组合的详细摘要写入文件。那很好

2>
输出我真正需要的一行信息。但当执行时,我得到:

Alt_moabit  10  0
total   47,8221 50,6329 50,1031
Alt_moabit  10  1
total   47,8408 49,9973 49,8197
Alt_moabit  12  0
total   47,0665 50,1457 49,6755
Alt_moabit  12  1
total   47,1193 49,4284 49,3476
然而,我想要的是:

Alt_moabit  10  0    total  47,8221 50,6329 50,1031
Alt_moabit  10  1    total  47,8408 49,9973 49,8197
Alt_moabit  12  0    total  47,0665 50,1457 49,6755
Alt_moabit  12  1    total  47,1193 49,4284 49,3476
我怎样才能做到这一点


(如果您认为有更合适的标题,请随时更改标题)

您可以将
-n
选项传递给第一个
echo
命令,这样它就不会输出换行符


作为一个快速演示,以下内容:

echo "test : " ; echo "blah"
将为您提供:

test : 
blah
在两个输出之间使用换行符


同时,对于第一个
回音,使用
-n

echo -n "test : " ; echo "blah"
将获得以下输出:

test : blah

在两个输出之间没有任何换行符。

echo实用程序的(GNU版本)有一个-n选项来省略尾随的换行符。在你的第一次回音中使用它。您可能需要在第一行之后或第二行之前留出一些空间以便于阅读。

您可以使用
printf
而不是
echo
,也就是说,

printf是解决问题的正确方法(+1 kurumi),但为了完整性,您也可以:

echo "$file\t$qp\t$i $( ./command 2>&1 > $file-$qp-psnr.txt )" >> psnr.txt echo“$file\t$qp\t$i$(./command 2>&1>$file-$qp-psnr.txt)”>>psnr.txt
我想没有正确的方法。drysam的解决方案对我有效,他是第一个回答的人。不过,谢谢你的这句有趣的台词!:)也许“正确”太强了,但printf将比echo-n更便携。这正是我今天需要的!谢谢