Vik Oliver was asking how to chop up text files

Here is a bash script which I created to do this.

Line wrapping has messed things up a bit.

Download the script in text file form to avoid wrapping from;

http://www.inet.net.nz/~atobrent/readit.txt

Comments, improvements appreciated

Cheers Ross Drummond

Start here--------------------------------------------------------------------
#! /bin/bash
declare -i StartNumber
declare -i FinishNumber

cat << EndOfText
A script which assists you to output to standard
output a range of lines from a file you specify.
EndOfText

echo -e "\nType in the name of the file you wish to extract lines from. \nThen 
press <ENTER>"
read FileName

echo -e "Type the number of the line where you want to start.\nThen press 
<ENTER>."
read StartNumber

echo -e "Type the number of the line where you want to finish\nThen press 
<ENTER>."
read FinishNumber

if [ ! -f "$FileName" ]
then
        echo -e "Error. File does not exist.\nScript terminating."
        exit 0
fi

if [[ $StartNumber -ge $FinishNumber ]]
then
        echo -e "Error. Start line number is greater than or equal to finish line 
number.\nScript terminating."
        exit 0
fi

TotalLines=`wc -l $FileName | cut -d \t -f 1`

if [[ $FinishNumber -gt $TotalLines  ]]
then
        echo -e "Error. The finish line number is greater than the number of lines in  
the file.\nScript terminating."
        exit 0
fi

cat -n $FileName |head -n $FinishNumber | tail -n 
$((($FinishNumber-$StartNumber)+1))

End here---------------------------------------------------------------------

Reply via email to