Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe to Linux split command?

Tags:

linux

split

pipe

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!!

like image 875
schoon Avatar asked Feb 23 '17 12:02

schoon


People also ask

How do I pipe a command in Linux?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do I split a line in Linux?

If you want your file to be split based on the number of lines in each chunk rather than the number of bytes, you can use the -l (lines) option. In this example, each file will have 1,000 lines except, of course, for the last one which may have fewer lines.

How do you split a file into parts in Linux?

To split large files into small pieces, we use the split command in the Linux operating system. The split command is used to split or break large files into small pieces in the Linux system. By default, it generates output files of a fixed size, the default lines are 1000 and the default prefix would be 'x'.

How does split work in Linux?

Split command in Linux is used to split large files into smaller files. It splits the files into 1000 lines per file(by default) and even allows users to change the number of lines as per requirement. The names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on.


1 Answers

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.

like image 132
hek2mgl Avatar answered Oct 07 '22 01:10

hek2mgl