# Lunix split with ruby

**URL:** <https://www.ruby-forum.com/t/lunix-split-with-ruby/176874>\
**Category:** Ruby\
**Created:** [October 7, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874 "2009-10-07T12:18:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/1 "2009-10-07T12:18:14Z")

</div>

I am using cygwin on windows.  
I write this script. I have to split all files in the  
directory.

Dir.foreach(‘C:/tmp/test’) {|file|  
f = system( “split -l 60000 #{file}”)  
puts “file name : #{f}”  
}

My files is  
file1.txt 10mb  
file2.txt 14mb  
file3.txt 8mb  
file4.txt 5mb

I want to split the files  
file1-1.txt  
file1-2.txt  
file1-3.txt  
…

file4-1.txt  
file4-2.txt

how can i do it ?

please help me.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/2 "2009-10-07T17:25:30Z")

</div>

unknown wrote:

> On Wed, Oct 7, 2009 at 6:19 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > file1-1.txt  
> > file1-2.txt  
> > file1-3.txt  
> > …
> > 
> > how can i do it ?
> > 
> > please help me.
> 
> What (incorrect) output does your current script produce?

the output is only  
xaa  
xab  
xac  
xad  
…

I want to loop all files and split for each around 1mb.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/3 "2009-10-07T17:19:35Z")

</div>

On Wed, Oct 7, 2009 at 6:19 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> file1-1.txt  
> file1-2.txt  
> file1-3.txt  
> …
> 
> how can i do it ?
> 
> please help me.

What (incorrect) output does your current script produce?

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/4 "2009-10-07T17:56:59Z")

</div>

unknown wrote:

> On Wed, Oct 7, 2009 at 11:25 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > please help me.  
> > > > I want to loop all files and split for each around 1mb.  
> > > > Okay, that helps; looks like by default split names the files  
> > > > “xaa”…“xzz”:
> 
> [http://unixhelp.ed.ac.uk/CGI/man-cgi?split](http://unixhelp.ed.ac.uk/CGI/man-cgi?split)
> 
> As as first pass (still won’t be quite what you asked for) try changing:
> 
> “split -l 60000 #{file}”
> 
> to:
> 
> “split -l 60000 #{file} #{file}-”
> 
> that should change the output file names from “x” + “#{counter}” to  
> “#{file}” + “-” + “#{counter}”

thank you for quick reply. I cold not understand counter. Where is it  
coming?  
like this counter = 0  
and counter file + 1 ???

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/5 "2009-10-07T17:40:41Z")

</div>

On Wed, Oct 7, 2009 at 11:25 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > please help me.  
> > > I want to loop all files and split for each around 1mb.  
> > > Okay, that helps; looks like by default split names the files  
> > > “xaa”…“xzz”:

[http://unixhelp.ed.ac.uk/CGI/man-cgi?split](http://unixhelp.ed.ac.uk/CGI/man-cgi?split)

As as first pass (still won’t be quite what you asked for) try changing:

“split -l 60000 #{file}”

to:

“split -l 60000 #{file} #{file}-”

that should change the output file names from “x” + “#{counter}” to  
“#{file}” + “-” + “#{counter}”

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 8, 2009, 3:30am UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/6 "2009-10-08T03:30:03Z")

</div>

unknown wrote:

> On Wed, Oct 7, 2009 at 11:57 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > thank you for quick reply. I cold not understand ï¿½counter. Where is it  
> > coming?  
> > like this counter = 0  
> > and ï¿½counter file + 1 ???
> 
> Sorry, yes that was unclear; it was just representing the “aa”…“az”  
> that split tacks on to the end of the file name. So, I’m thinking  
> that will produce output files named like:
> 
> ```
> file1.txt-aa
> file1.txt-ab
> file1.txt-ac
> file2.txt-aa
> file3.txt-ab
> ...
> 
> ```

sorry for late reply.  
I was thinking like that but it was not work for me.  
thank you for help.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 7, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/7 "2009-10-07T18:10:33Z")

</div>

On Wed, Oct 7, 2009 at 11:57 AM, Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> thank you for quick reply. I cold not understand counter. Where is it  
> coming?  
> like this counter = 0  
> and counter file + 1 ???

Sorry, yes that was unclear; it was just representing the “aa”…“az”  
that split tacks on to the end of the file name. So, I’m thinking  
that will produce output files named like:

```
file1.txt-aa
file1.txt-ab
file1.txt-ac
file2.txt-aa
file3.txt-ab
...

```

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 8, 2009, 7:06am UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/8 "2009-10-08T07:06:50Z")

</div>

thank you very much.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [October 8, 2009, 4:27am UTC](https://www.ruby-forum.com/t/lunix-split-with-ruby/176874/9 "2009-10-08T04:27:39Z")

</div>

Ahmet K. wrote:

> > ```
> > file1.txt-aa
> > file1.txt-ab
> > file1.txt-ac
> > file2.txt-aa
> > file3.txt-ab
> > ...
> > 
> > ```
> 
> sorry for late reply.  
> I was thinking like that but it was not work for me.  
> thank you for help.

use  
split -d -l 60000 ${file} ${file}-  
which should give you files of the format  
$file-01  
$file-02  
$file-0…

if the split file is going to generate more than 99 files, add -a 4 to  
the parameters…

i.e.  
split -d -l 60000 MyFile.txt MyFile.txt-  
gives  
MyFile.txt-01  
MyFile.txt-02  
MyFile.txt-03  
MyFile.txt-…

Then write a script that renames the files ala

for file in `ls MyFile.txt-*`  
do  
prefix=${file%%.txt\*}

# echo $prefix

```
 suffix=${file##file.txt}

```

# echo $suffix

```
 echo ${prefix}${suffix}.txt
 mv $file ${prefix}${suffix}.txt

```

done
