Re: [SLUG] Slicing a file

From: Sick Twist (thesicktwist@hotmail.com)
Date: Fri May 26 2006 - 07:49:59 EDT


>From: Paul M Foster <paulf@quillandmouse.com>
>Reply-To: slug@nks.net
>To: slug@nks.net
>Subject: Re: [SLUG] Slicing a file
>Date: Fri, 26 May 2006 02:17:52 -0400
>
>(Note: I'm not using a second file to tell me where the splits should be.)
>
>A (real) example of the above would be (assuming original file contains
>10000 lines, and needs splits of 2000, 3000, 3000 and 2000):
>
>
>head -n 2000 original > result1
>tail -n 8000 original > temp1
>head -n 3000 temp1 > result2
>tail -n 5000 temp1 > temp2
>head -n 3000 temp2 > result3
>tail -n 2000 temp2 > result4
>
>The above will work, but leaves temp files potentially all over the place
>(temp1 and temp2). If necessary, I can do that. But it would be better to
>create the exact "chunks" from a file without any extra files lying around.
>
>--
>Paul M. Foster

Hope the following little hack does what you need:
-Jonathon

#!/bin/bash
# slice splits one file into files of various lengths
#Copyright (C) 2006 Jonathon Conte
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

PREFIX="part" # prefix to use for new files
PROG="${0##*/}" # name of program

err() { echo "$PROG: $*" >&2; }
usage()
{
        echo "Usage: $PROG N[...] FILE" >&2
        echo "Split FILE into files of N lines." >&2
        echo "A new file will be created for each value of N passed to" >&2
        echo "${PROG} regardless of the number of lines in FILE. Each" >&2
        echo "value of N must be an integer greater than 0." >&2
}

# check for parameters
if [ $# -lt 2 ]; then
        usage
        exit 1
fi

# make sure last parameter is an existant file with read permission
eval FILE="$"$#""
if [ ! -r "$FILE" ]; then
        if [ -e "$FILE" ]; then
                err "you do not have permission to read \"$FILE\""
                exit 1
        fi
        err "the file \"$FILE\" does not exist"
        exit 1
fi

# make sure all other parameters are valid integers
ARG=1
while [ $ARG -lt $# ]; do
        eval ARGV="$"$ARG""
        if [ "$ARGV" -lt 1 ]; then
                usage
                exit 1
        fi
        ARG=$(($ARG+1))
done

# split the file
START=1
ARG=1
while [ $ARG -lt $# ]; do
        eval ARGV="$"$ARG""
        tail -n +$START "$FILE" | head -$ARGV > "${PREFIX}${ARG}"
        START=$(($START+$ARGV))
        ARG=$(($ARG+1))
done

exit 0

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:23:13 EDT