- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: script to split large file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 10:02 PM
тАО09-01-2002 10:02 PM
Could someone help me with a script im trying to write to split a large file into smaller files of 450 lines each. The output files (split files) could have the same name as the input file with a .01 .02 .03 and so on extension. or it could be an xx00 or xx01 using csplit.... Here is something i tried but it works only until it reaches the first 450 lines...Could somebody help me to set a counter and keep splitting till end of file....
##############################
#!/bin/ksh
echo "Enter file name to split :"
read infile
line=$(cat $infile | wc -l)
if(($line>450))
then
csplit $infile 451
fi
#############################
Thanks
George
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 10:08 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 10:15 PM
тАО09-01-2002 10:15 PM
Re: script to split large file
# /usr/bin/split -l 450
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 11:25 PM
тАО09-01-2002 11:25 PM
Re: script to split large file
you can insert a counter like this:
#!/bin/ksh
typeset -i TOTAL_LINES=0 SPLIT_LINES=0 LINE_LIMIT=0
OUTFILE=/tmp/georgeout
# No. of lines per file
LINE_LIMIT=450
echo "Enter file name to split :"
read INFILE
TOTAL_LINES=$(cat $INFILE | wc -l)
while [ $SPLIT_LINES -lt $TOTAL_LINES ]
do
split -l ${LINE_LIMIT} $INFILE $OUTFILE
SPLIT_LINES=$(( $SPLIT_LINES + $LINE_LIMIT ))
done
hope it helps.
Regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 11:26 PM
тАО09-01-2002 11:26 PM
Re: script to split large file
You can use split in order to do the split and then use mv to change the name:
/usr/bin/split -l 450 filename
integer counter=0
for i in filename.*
do
(( counter = $counter + 1 ))
nomfile=$(echo $i | cut -d"." -f1)
mv $i $nomfile.$counter
done
Regards,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 11:53 PM
тАО09-01-2002 11:53 PM
Re: script to split large file
I would do this with awk. Try this...
#!/bin/ksh
$filename = filename_to_be_split.txt
awk '
BEGIN {
count = 0
filecount = 1
outfile = ARGV[1] "." filecount
}
{
count++
if ( count >= 451 ) {
filecount++
outfile = ARGV[1] "." filecount
count = 1
}
print > outfile
}' $filename
I hope that helps,
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2002 11:57 PM
тАО09-01-2002 11:57 PM
Re: script to split large file
I went into some perl/awk/shell combined mode on the second line there. It should read:
filename=file_to_be_split.txt
where file_to_be_split.txt is the file you want split.
Simon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2002 01:41 AM
тАО09-02-2002 01:41 AM
Re: script to split large file
I think your original option of using csplit may have worked if you added the -k option. (Were you getting "out of range" messages ?)
The option helps when you need to specify an arbitarily large repeat argument as otherwise new files are removed.
eg - csplit -k $infile 450 {99}
The {99} should allow up to 100 files called .xx00 thru .xx99 to be created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2002 02:53 AM
тАО09-02-2002 02:53 AM
Re: script to split large file
Why not use "split"
split [-l line_count] [-a suffix_length] [file [name]]
do a man on split.
CSPLIT does split'ing on CONTEXT, aka string matches.
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2002 03:32 AM
тАО09-02-2002 03:32 AM
Re: script to split large file
In most cases split will do this job, but if you for some reason will make your own "split" this can be done with the "exec" command. Here is one example (filesize is 10 lines but this can be changed at your own need).
let line=1
let cntr=1
file=x
exec >$file$cntr
while read a
do
echo $a
if [ $line -eq 10 ]
then
let cntr=$cntr+1
line=0
exec >$file$cntr
fi
let line=$line+1
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-02-2002 08:36 PM
тАО09-02-2002 08:36 PM