The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to search for text in a file and copy file imeadows UNIX for Dummies Questions & Answers 9 11-12-2008 09:12 PM
Script to capture new lines in a file and copy it to new file fara_aris Shell Programming and Scripting 0 05-27-2008 10:11 PM
How to copy file through ssh tikka123 Shell Programming and Scripting 2 03-18-2008 04:40 AM
Copy File JuergenW UNIX for Dummies Questions & Answers 2 09-09-2002 08:17 AM
using C to copy a file ... abdul High Level Programming 1 09-11-2001 08:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2001
Enda Martin Enda Martin is offline
Registered User
  
 

Join Date: Jun 2001
Posts: 3
% File Copy

is there a command within unix which will allow you to copy a certain % of the file to a new file.

EG : File a --> Copy 65% of a to give File b
  #2 (permalink)  
Old 08-29-2001
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
negative. your best bet at something like that would be write a script that will do:

Variable=line count / 2
processing line by line incrimenting till new variable is = variable
then dumping to file
  #3 (permalink)  
Old 08-30-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
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 06:34 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0