0

I created a for loop that goes through multiple files and outputs the results into one file:

for x in /home/moleculo/x*; do ExtractOutCalls2.sh /home/Scripts/000 $x & done

So each of my input files starts with letter x, that's x* as input. Script takes each of those input files $x and outputs to file /home/Scripts/000

Now I have a question:

if this is done on a few thousand files, is it a good way to put like this?

also if I use multiple input files, but specify one output file, will this mean, that my output will won't be appended? If not, how to do it

Regards, Irek

2 Answers 2

1

Yes, your output file gets overwritten by each process. Make each script output to its own file, and once all the scripts are finished, concatenate the output:

i=0
for x in /home/moleculo/x* ; do
    ExtractOutCalls2.sh /home/Scripts/000 $x > OUT.$i &
    (( i++ ))
done
wait
cat OUT.* > OUT
rm OUT.*

You have to change the script to output to standard output instead of the file, or make it accept the name of the output file to be created.

Sign up to request clarification or add additional context in comments.

3 Comments

I have the outptu as $argv[1] so this is doing the trick. And if this is done in parallel on couple of thousand of files, how does it look in terms of my cores? Won't it crash?
@Irek: Do you have thousands of cores? The disk I/O would probably be the bottleneck, though. Do not run thousands of processes.
Well actually, I do, but I just want to run it smart. But even if I do only 1 file input, will sth like xargs -P 4 -n 1 script.sh speed it? Or is it still no good, since I am going with only 1 file
1

Often you can use the file - to designate stdout:

for x in /home/moleculo/x*; do ExtractOutCalls2.sh - $x & done

To avoid mixing output use GNU Parallel:

parallel ExtractOutCalls2.sh - {} ::: /home/moleculo/x* > output

1 Comment

The second one is better and cleaner.

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.