Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send output from `split` utility to stdout

Tags:

bash

split

stdout

From this question, I found the split utilty, which takes a file and splits it into evenly sized chunks. By default, it outputs these chunks to new files, but I'd like to get it to output them to stdout, separated by a newline (or an arbitrary delimiter). Is this possible?

I tried cat testfile.txt | split -b 128 - /dev/stdout

which fails with the error split: /dev/stdoutaa: Permission denied.

Looking at the help text, it seems this tells split to use /dev/stdout as a prefix for the filename, not to write to /dev/stdout itself. It does not indicate any option to write directly to a single file with a delimiter. Is there a way I can trick split into doing this, or is there a different utility that accomplishes the behavior I want?

like image 901
ecapstone Avatar asked Jul 28 '16 19:07

ecapstone


3 Answers

It's not clear exactly what you want to do, but perhaps the --filter option to split will help out:

   --filter=COMMAND
          write to shell COMMAND; file name is $FILE

Maybe you can use that directly. For example, this will read a file 10 bytes at a time, passing each chunk through the tr command:

split -b 10 --filter "tr [:lower:] [:upper:]" afile

If you really want to emit a stream on stdout that has separators between chunks, you could do something like:

split -b 10 --filter 'dd 2> /dev/null; echo ---sep---' afile

If afile is a file in my current directory that looks like:

the quick brown fox jumped over the lazy dog.

Then the above command will result in:

the quick ---sep---
brown fox ---sep---
jumped ove---sep---
r the lazy---sep---
 dog.
---sep---
like image 181
larsks Avatar answered Sep 21 '22 12:09

larsks


From info page : `--filter=COMMAND' With this option, rather than simply writing to each output file, write through a pipe to the specified shell COMMAND for each output file. COMMAND should use the $FILE environment variable, which is set to a different output file name for each invocation of the command.

split -b 128 --filter='cat ; echo ' inputfile
like image 43
Sam Daniel Avatar answered Sep 19 '22 12:09

Sam Daniel


Here is one way of doing it. You will get each 128 character into variable "var".

You may use your preferred delimiter to print or use it for further processing.

#!/bin/bash

cat yourTextFile | while read -r -n 128 var  ; do
    printf "\n$var"
done

You may use it as below at command line:

while read -r -n 128 var  ; do printf "\n$var" ; done < yourTextFile
like image 31
Robert Ranjan Avatar answered Sep 20 '22 12:09

Robert Ranjan