PDA

View Full Version : Need a shell script(to split the file)



carteris21
September 20th, 2006, 10:07 PM
Hi,
my someone could write sh script, that splitting file in n bytes portions, the same what do split command, but without split command.
I can't find any command which can read some interval of bytes from the file.

LotsOfPhil
September 20th, 2006, 10:48 PM
Why can't you use split? Would it be okay to split it up every n lines?

carteris21
September 21st, 2006, 06:53 AM
It's my task :). I think it would be ok, can you write an example.

amo-ej1
September 21st, 2006, 09:29 AM
use dd (man dd)



dd if=INPUTFILE of=OUTPUTFILE bs=BLOCKSIZE count=CNT skip=SKIP


reads CNT blocks of BLOCKSIZE large rom INPUTFILE and write them to OUTPUTFILE skipping SKIP times BLOCKSIZE from the beginning.

TheMono
September 21st, 2006, 09:36 AM
Irrelevant Comment: Nice avatar, amo-ej1... Very cool.

LotsOfPhil
September 21st, 2006, 03:20 PM
Hi,
my someone could write sh script, that splitting file in n bytes portions, the same what do split command, but without split command.
I can't find any command which can read some interval of bytes from the file.

Well, here is what I have. It isn't exactly what you want, but should point you on the right track.



#!/bin/sh
#
# Split a file
#
if [ $# -lt 2 ]
then
echo "Syntax is ./lineSplit.sh <file name> <split line number>"
echo "Example: ./lineSplit.sh bp.out 25000"
exit 1
fi
file=$1
split=$2
lineMax=`wc -l $file | awk '{print $1}'`
counter=1
i=1
while [ $counter -lt $lineMax ]
do
split1=`expr $counter`
split2=`expr $counter + $split - 1`
sed -n "$split1","$split2"p $file > fragment."$i"
i=`expr $i + 1`
counter=`expr $split2 + 1`
done

sed -n "$counter",\$p $file > fragment."$i"


Hmm, what needs explanation...
You can chuck the if statement at the beginning if you want. Just set "file" and "split" to what you want.
I think there should be a better way to get the value of lineMax, but I am blanking on it.

If anything is unclear about it, or if you can improve it for me, let me know.

carteris21
September 21st, 2006, 05:13 PM
Thank you :).