schoon
schoon

Reputation: 3324

How do I pipe to Linux split command?

I'm a bit useless at Linux CLI, and I am trying to run the following commands to randomly sort, then split a file with output file prefixes 'out' (one output file will have 50 lines, the other the rest):

sort -R somefile  | split -l 50 out

I get the error

split: cannot open ‘out’ for reading: No such file or directory

this is presumably because the third parameter of split should be its input file. How do I pass the result of the sort to split? TIA!!

Upvotes: 12

Views: 20614

Answers (3)

Joao Neto
Joao Neto

Reputation: 340

For POSIX systems like mac os the - parameter is not accepted and you need to completely omit the filename, and let it generate it's own names.

sort -R somefile | split -l 50

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157967

Use - for stdin:

sort -R somefile  | split -l 50 - out

From man split:

Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default size is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUT is -, read standard input.

Allowing - to specify input is stdin is a convention many UNIX utilities follow.

Upvotes: 22

fzgregor
fzgregor

Reputation: 1897

out is interpreted as input file. You can should a single dash to indicate reading from STDIN:

sort -R somefile | split - -l 50 out

Upvotes: 6

Related Questions