Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change where unix pipe sends its results for the split command?

Basically I'm doing this:

export_something | split -b 1000

which splits the results of the export into files names xaa, xab, xac all 1000 bytes each

but I want my output from split to go into files with a specific-prefix. Ordinarily I'd just do this:

split -b <file> <prefix>

but there's no flag for prefix when you're piping to it. What I'm looking for is a way to do this:

export_something | split -b 1000 <output-from-pipe> <prefix>

Is that possible?

like image 242
Matthew Rathbone Avatar asked Dec 03 '10 15:12

Matthew Rathbone


People also ask

How to split a file in Unix based on line number?

If you use the -l (a lowercase L) option, replace linenumber with the number of lines you'd like in each of the smaller files (the default is 1,000). If you use the -b option, replace bytes with the number of bytes you'd like in each of the smaller files.

How do I split a single file into multiple files in Unix?

To split a file into pieces, you simply use the split command. By default, the split command uses a very simple naming scheme. The file chunks will be named xaa, xab, xac, etc., and, presumably, if you break up a file that is sufficiently large, you might even get chunks named xza and xzz.

What connects the output of one process to the input of a second?

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. It can also be visualized as a temporary connection between two or more commands/ programs/ processes.

What is use of split command?

The split command reads the specified file and writes it in 1000-line pieces to a set of output files.


2 Answers

Yes, - is commonly used to denote stdin or stdout, whichever makes more sense. In your example

export_something | split -b 1000 - <prefix>
like image 181
dennycrane Avatar answered Sep 26 '22 06:09

dennycrane


Use - for input

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.

export_something | split -b 1000 - <prefix>
like image 24
The Archetypal Paul Avatar answered Sep 26 '22 06:09

The Archetypal Paul