Parallelize STDIN
suggest changeNow, let’s imagine we have 1 large file (e.g. 30 GB) that needs to be converted, line by line. Say we have a script, convert.sh, that does this <task>. We can pipe contents of this file to stdin for parallel to take in and work with in chunks such as
<stdin> | parallel --pipe --block <block size> -k <task> > output.txt
where <stdin> can originate from anything such as cat <file>.
As a reproducible example, our task will be nl -n rz. Take any file, mine will be data.bz2, and pass it to <stdin>
bzcat data.bz2 | nl | parallel --pipe --block 10M -k nl -n rz | gzip > output.gz
The above example takes stdin from bzcat data.bz2 | nl. The first nl is only there to show that the final output.gz keeps the original order. Then parallel divides stdin into 10 MB chunks, and for each chunk runs nl -n rz, which appends a right-justified number (see nl --help). --pipe tells parallel to split stdin into multiple jobs, and --block sets the block size. -k keeps the order.
Your final output should look something like
000001 1 <data>
000002 2 <data>
000003 3 <data>
000004 4 <data>
000005 5 <data>
...
000587 552409 <data>
000588 552410 <data>
000589 552411 <data>
000590 552412 <data>
000591 552413 <data>
My original file had 552,413 lines. The first column represents the parallel jobs, and the second column represents the original line numbering that was passed to parallel in chunks. You should notice that the order in the second column (and rest of the file) is maintained.