1833332 Members
2893 Online
110051 Solutions
New Discussion

Scrpiting

 
Anoop P_2
Regular Advisor

Scrpiting

When we execute two commands in a single script and append the output in to a single file, the output comes like this:

#script.sh
Command1 >> output
Command2 >> output

# cat output
output of command1
output of command2

But if I want it this way:

output of command1 output of command2

how should I write the script 'script.sh'?

Apperciate immediate help!

Anoop
9 REPLIES 9
RAC_1
Honored Contributor

Re: Scrpiting

Command1 >> output_1
Command2 >> output_2

paste output_1 output_2 >> final_output.
Man paste for details.

Anil

There is no substitute to HARDWORK
Mark Grant
Honored Contributor

Re: Scrpiting

If you have complete control of command1 and command2 and they are only writing a single line each you can do it.

Assuming that command1 is using "echo" to output it's data then use a "\c" to stop the output of the carriage return e.g

echo "here is the data\c"

And command2 can just do an echo normally.

Never preceed any demonstration with anything more predictive than "watch this"
Steve Steel
Honored Contributor

Re: Scrpiting

Hi

If output is doable

echo $(Command1)$(Command2) >> output


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Chris Vail
Honored Contributor

Re: Scrpiting

I'm not sure what you're asking. If you want the output of both commands on the same line, this is easy.

echo "result of command1 \c">>file
echo "result of command2">>file

The \c in the first echo statement suppresses the carriage return that echo normally appends. The second command gets the carriage return.


Chris
Hein van den Heuvel
Honored Contributor

Re: Scrpiting


btw... do you want a space between the lines?

you could join the lines after the fact with and editor or awk. For examples:

echo "1,2 j\nw" | ed x

The usage advantage of an editor is that you don't have to deal with an intermediate file.

With intermediate file and with space:

Command1 >> tmp
tr "[\012]" "[ ]" output
Command2 >> output

Without space:

Command1 >> tmp
tr -d "[\012]" output
Command2 >> output

Or you could fix the problem after the fact:

fix up output with perl with space:

perl -e '$_ = <>; chop; print $_ ." ".<>' output

fix up output with 'ed' without space:
echo "1,2 j\nw" | ed -s output

So many options!
:-)

Hein.





Or you cou


Rory R Hammond
Trusted Contributor

Re: Scrpiting


Not sure what the intent is.

This method appends output of command1 to the bottom of command2
(
command1
command2 ) > output

If the output is multi line and you want them side by side. Paste works
command1 > output1
command2 > output2
paste output1 output2 > output

If you want the output side by side for comparision sdiff does a nice job of delimiting the changes.
command1 > output1
command2 > output2
sdiff output1 output2 > output

If the commands return one line of output and you want them side by side.
echo "$(command1)\c $(command2)" > output
There are a 100 ways to do things and 97 of them are right
Anoop P_2
Regular Advisor

Re: Scrpiting

Thank you for every one.

Infact I wanted it side by side to make a list of servers and their sendmail versions, to be created through a script.

Paste has worked well.

Regards,
Anoop
Rodney Hills
Honored Contributor

Re: Scrpiting

You could also use "pr -m output1 output2". Do a "man pr" to see other options available to help format the output.

HTH

-- Rod Hills
There be dragons...
Anoop P_2
Regular Advisor

Re: Scrpiting

The solution has already been obtained