开发者

Concatenate output of two commands into one line

开发者 https://www.devze.com 2023-02-19 20:59 出处:网络
I have a very basic shell script here: for file in Alt_moabit Book_arrival Door_flowers Leaving_laptop

I have a very basic shell script here:

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

command calculates some PSNR values and writes a detailed summary to a file for each combination of file, qp and i. That's fine.

The 2>> outputs one line of information that I really need. But when executed, I get:

Alt_moabit  10  0
total   4开发者_如何转开发7,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

What I want, however, is this:

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

How can I achieve that?

(Please feel free to change the title if you think there's a more appropriate one)


You could pass the -n option to your first echo command, so it doesn't output a newline.


As a quick demonstration, this :

echo "test : " ; echo "blah"

will get you :

test : 
blah

With a newline between the two outputs.


While this, with a -n for the first echo :

echo -n "test : " ; echo "blah"

will get you the following output :

test : blah

Without any newline between the two output.


The (GNU version of) echo utility has a -n option to omit the trailing newline. Use that on your first echo. You'll probably have to put some space after the first line or before the second for readability.


You can use printf instead of echo, which is better for portability reasons.


printf is the correct way to solve your problem (+1 kurumi), but for completeness, you can also do:

   echo "$file\t$qp\t$i $( ./command 2>&1 > $file-$qp-psnr.txt )" >> psnr.txt      


While echo -n may work if you just want the print the output to console, it won't work if you want the output redirected to file.

If you want the concatenated output to be redirected to a file, this will work:

echo "Str1: `echo "Str2"`" >> file


I was also facing the same problem. To define my problem, I have a script which is using the echo function like this:

echo -n "Some text here"
echo -n "Some text here"

The output I was getting is like this:

-n Some text here
-n Some text here

and I want the text to be in same line and it is also printing -n option in the output.

Note :- According to man Page, -n option do not print the trailing newline character.

The way I solved it using by adding the shebang in the starting of the script file like this.

#!/bin/bash
echo -n "Some text here"
echo -n "Some text here"

This will print the desired output like this:

Some text here Some text here

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消