lunix split with ruby

A

Ahmet Kilic

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.
 
B

brabuhr

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|
=A0f =3D system( "split -l 60000 #{file}")
=A0 =A0puts "file =A0name : #{f}"
}

I want to split the files
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?
 
A

Ahmet Kilic

unknown said:
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.
 
B

brabuhr

the output is only
xaa
xab
xac
xad
...

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

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}"
 
A

Ahmet Kilic

unknown said:
Okay, that helps; looks like by default split names the files
"xaa".."xzz":

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 ???
 
B

brabuhr

thank you for quick reply. I cold not understand =A0counter. Where is it
coming?
like this counter =3D 0
and =A0counter 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
...
 
A

Ahmet Kilic

unknown said:
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.
 
R

Reid Thompson

Ahmet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top