Wie kombiniert man in bash die Ausgabe zweier Befehle und fügt sie in der SAME-Zeile einer Datei ein?

2919
Galaxy

Zum Beispiel habe ich hier zwei Befehle:

{ command1 & command2; } >> file1 

Zum Beispiel ist die Ausgabe von command1ist 400und die Ausgabe von command2ist 4.

Das ist was ich bekomme:

400 4 

Ich möchte, dass die Ausgaben der beiden Befehle in derselben Zeile wie folgt angehängt werden :

400 4 
0
Bist du sicher, dass du diese Ausgabe bekommst? Sie stellen command1 in den Hintergrund. Woher wissen Sie dann, dass Sie garantiert die Ausgabe zuerst erhalten? glenn jackman vor 7 Jahren 0

2 Antworten auf die Frage

1
Zoredache

Your command basically is this.

$ (echo '400' && echo '4') | hexdump -C 00000000 34 30 30 0a 34 0a |400.4.| 00000006 

Not the output includes the end of line \n aka 0a characters. So one easy thing you could do is pipe that through a command that will delete the \n.

So something like this

$ (echo '400' && echo '4') | tr '\n' ' ' | hexdump -C 00000000 34 30 30 20 34 20 |400 4 | 00000006 

Which has actual output of 400 4. But that doesn't include any line endings so you may want to only remove the line ending from the first command.

$ (echo '400' | tr '\n' ' ' && echo '4') | hexdump -C 00000000 34 30 30 20 34 0a |400 4.| 00000006 

Anyway, the point is that the line ending is just a character use tr, sed, awk, or your favorite tool that lets you do manipulation of the string.

One other option that may work, depending on your requirements may be to do something like below. With this, the output of the commands are magically stripped of the EOL for you, but an EOL is appended by the echo command.

$ echo "$(echo '400') $(echo '4')" | hexdump -C 00000000 34 30 30 20 34 0a |400 4.| 00000006 
Ich möchte nicht "400" wiederholen. Der 400 ist nur ein Platzhalter für die Ausgabe eines Befehls. Galaxy vor 7 Jahren 0
Ich weiß, ersetzen Sie das 'Echo 400' durch Ihren Befehl. Ich habe das als Beispiel genommen. Echo war nur ein nützlicher Befehl **, mit dem ich schnell Beispiele erstellen konnte, die Ergebnisse lieferten, die genau den Ergebnissen Ihrer Frage entsprachen. Zoredache vor 7 Jahren 1
Es gibt auch: `paste <(cmd1) <(cmd2) >> file.txt` Seb B. vor 7 Jahren 0
1
glenn jackman

I'd use paste

{ command1 & command2; } | paste -d" " -s >> file1 

(concern about backgrounding command1 has been noted)

Demo:

$ { echo 400; echo 4; } 400 4 $ { echo 400; echo 4; } | paste -d" " -s 400 4