0

I'm running a large loop that calls a program that outputs a single value. I would like to redirect that output to a text file appended by a space rather than a new line. Is there any way to do this?

for i in {1..1000000000}; do
    mincstats file${i}.mnc -mean -quiet >> output.txt
done

I have tried assigning the output to a variable as below, but I think this may be taking up unnecessary processing time. What would be the most efficient way to do this?

for i in {1..1000000000}; do
    var=$(mincstats file${i}.mnc -mean -quiet)
    echo -n $var >> output.txt
done
1
  • echo is a built-in; it's not going to use any significant amount of time over what mincstats already requires. Commented Nov 6, 2015 at 20:51

2 Answers 2

2

You could translate all the newlines to space.

It's also more efficient to do the redirection on the whole loop, instead of each command.

for i in {1..1000000000}; do
    mincstats file${i}.mnc -mean -quiet
done | tr '\n' ' ' >> output.txt
Sign up to request clarification or add additional context in comments.

2 Comments

yep think this is the best way.
Thanks! both suggestions work, but this seems to run a little bit faster.
1

Try this:

for ((i=1;i<=1000000000;i++)); do 
  echo -n "$(mincstats file${i}.mnc -mean -quiet) "
done > output.txt

1 Comment

You'll echo a newline after the space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.