simlmx
simlmx

Reputation: 1333

Bash command with multiple input files

In bash, is there a way to do some kind of loop to pass multiple inputs to a command, i.e. to shorten something like

cmd < 1.txt < 2.txt < ... < 100.txt

My exact use-case is that I have a bunch of sorted gzipped files that I want to merge together into another gzipped file:

sort --merge <(zcat 1.txt.gz) <(zcat 2.txt.gz) ... <(zcat 100.txt.gz) | gzip > out.txt.gz

I want to avoid unziping them separately if possible.

Edit: I should note that the files are huge, and that's why I want to take advantage of the --merge option, and not just concatenate them and resort the whole thing.

Upvotes: 1

Views: 1184

Answers (1)

janos
janos

Reputation: 124646

You could do like this:

zcat *.txt.gz | sort -m | gzip > out.txt.gz

But as you pointed out, that's plain wrong. You're right, for sort -m to make sense you need multiple files.

Try this instead:

x=; for i in *.txt.gz; do x="$x <(zcat $i)"; done
eval "sort -m $x" | gzip > out.txt.gz

Upvotes: 2

Related Questions