Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate output of two commands into one line

Tags:

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   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 

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)

like image 821
slhck Avatar asked Mar 27 '11 11:03

slhck


People also ask

How do you combine two commands in Python?

Like I would do in OCAML. How can I execute multiple commands in one like in python? You can just use semicolons.

When we use to join 2 commands both the commands are executed?

If you want to execute all the commands, whether the previous one executes or not, you can use semicolon (;) to separate the commands. If you want to execute the next command only if the previous command succeeds, then you can use && to separate the commands.


1 Answers

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.

like image 101
Pascal MARTIN Avatar answered Sep 23 '22 07:09

Pascal MARTIN