1

For example I have two commands here:

{ command1 & command2; } >> file1

For example the output of command1 is 400, and the output of command2 is 4.

So this is what I get:

400
4

I want the outputs of the two commands to be appended on the same line like this:

400 4
Galaxy
  • 213
  • 2
  • 3
  • 10
  • Are you sure you're going to get that output? You put command1 into the background, so how do you know you're guaranteed to get its output first? – glenn jackman May 06 '16 at 22:14

2 Answers2

2

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
Zoredache
  • 19,828
  • 8
  • 50
  • 72
  • I do not want to echo "400" The 400 is just a placeholder for the output of a command. – Galaxy May 06 '16 at 21:39
  • 1
    I know, replace the 'echo 400' with your command. I was using that as an example. Echo was just a useful **command** that would let me quickly make examples that gave results that exactly matched what you had in your question. – Zoredache May 06 '16 at 21:40
  • There's also: `paste <(cmd1) <(cmd2) >> file.txt` – Seb B. May 06 '16 at 21:59
1

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
glenn jackman
  • 25,463
  • 6
  • 46
  • 69