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
Split files using Csplit savitha UNIX for Dummies Questions & Answers 7 12-01-2007 11:55 AM
Split A File Into 2 Files dummy_needhelp Shell Programming and Scripting 7 11-04-2007 07:36 PM
total number of files which have "aaa" in files whose names are File*_bbb* sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-16-2007 02:34 PM
Split a file into 2 or more files bobo UNIX for Dummies Questions & Answers 4 01-16-2006 05:15 PM
awk command to split in to 2 files m_subra_mani Shell Programming and Scripting 3 12-13-2005 05:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-13-2008
Migrainegirl Migrainegirl is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
split files into specified number of output files

Hi everyone,

I have some large text files that I need to split into a specific number of files of equal size. As far as I know (and I don't really know that much ) the split command only lets you specify the number of lines or bytes. The files are all of a different size, so the number of lines differ as well.
I use the following code to see how many lines each output file should be (1/50 of the original document):

y=`wc | awk '{ print $x/50}' | awk -F. '{if($2>=1){print $1+1}else{print $1}}'`
echo $y

The problem is that I can't use this variable $y as input for the split command like this:

split $x -d -l $y split/$x

It simply doesn't work. Does anyone know a way to pass the value of the $y variable into the split command? Or does anyone have a better way of solving the entire problem?

Thanks in advance!
  #2 (permalink)  
Old 05-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
It's not really clear why you can't use the variable. The way it is now, I don't think it contains a useful value (what does the echo print for you?) but fixing the scripts so it works sounds like the best plan unless you can explain why it "simply doesn't work".

Assuming $x is the name of the file you are about to split, try something like this instead.

Code:
y=`wc -l < $x | awk '{ print 1+int($1/50) }'`
I put in the addition of one on the theory that this should do something vaguely useful even with files which are too small, with a slight bias towards making the last file in the series smaller than the others. (Maybe divide by 49 instead to compensate?)
  #3 (permalink)  
Old 05-13-2008
Migrainegirl Migrainegirl is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
The echo gives me a number, which it's supposed to do.
The addition is used to round up the numbers: 231.2 becomes 232, but 231.0 stays 231.

I tried it again with the $x variable explicitly defined and then it worked. So the problem seems to be $x. (sorry, I didn't get much sleep last night ) This changes my question:
If I want to use my script with the original text file as input (like this: sh test.sh < file.txt), how do I call on the original file inside the script?
I obviously thought that the $x in 'split $x -d -l $y split/$x' would be replaced with 'file.txt', but it's not.

Last edited by Migrainegirl; 05-13-2008 at 07:53 AM..
  #4 (permalink)  
Old 05-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The positional parameters are in $1 $2 $3 etc but if you are invoking the script with redirection, there is no positional parameter, and no file name, just standard input.

Code:
vnix$ ./myscript file.txt oops   #  inside myscript, $1 is "file.txt" and $2 is "oops"
vnix$ ./myscript   # inside myscript, no file name is known; input is from standard input (terminal)
vnix$ ./myscript <file.txt   # again, inside myscript, no file name is known; input is redirected to come from file.txt

Last edited by era; 05-13-2008 at 08:02 AM.. Reason: Add enlightening (?) example
  #5 (permalink)  
Old 05-13-2008
Migrainegirl Migrainegirl is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Thanks!!
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 09:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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