The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM



Thread: % File Copy
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 08-30-2001
LivinFree's Avatar
LivinFree LivinFree is offline
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
My sloppy mess...

OK, I started thinking about it because it was bugging me, and tossed this together. Keep in mind that it is not very accurate, and gets less so the larger the file is. It rounds down, since I basically just chop all decimal remainders off, even if it's .99. And right now, it'll copy the bottom % of the file. If you want to copy the top of the file, change the _command variable from "tail" to "head". I don't know how portable this is, but it will give you an idea -
Now, without further ado - here goes:

#!/bin/sh
# copy percentage of file to another
# not very accurate, especially with files with a large
# number of lines...

_command="tail"
_from=$1
_per=$2
_to=$3
to_use () {
echo
echo "Usage: "
echo "`basename $0` file1 65 file2 "
echo "will copy (about) 65% of file1 to file2 "
echo
}
if [ "$#" != "3" ] ; then
to_use
exit 2
fi
if [ "$_per" -gt 100 -o "$_per" -lt 10 ] ; then
echo "Percentage must be between 10 and 100 "
echo
exit 2
fi
# special thanks to Faux_pseudo for helping here:
_fsize=`wc -l $_from | awk '{print $1}'`
_persize=`echo "$_fsize * 0.${_per}" | bc -q`
_fper=`echo $_persize | cut -d. -f1`
# Wheeee!
$_command -${_fper} ${_from} > ${_to}


Feel free to post any corrections or ideas. I'm always up to improve my scripting. It's pretty obvious to us all now that I'm not afraid to waste some time and resources!


Ooh.. Almost forgot to mention, it won't calculate anything less than 10% - I didn't want to throw the possibility of screwing something up in there, and I'm too tired to test it out any more. If it will work that way, you can change the "$_per -lt 10 to "_$per -lt 1.

--
LF

Last edited by LivinFree; 08-30-2001 at 03:34 AM.