Splitting a file into 4 files containing the same name pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a file into 4 files containing the same name pattern
# 1  
Old 04-16-2014
Splitting a file into 4 files containing the same name pattern

Hello,

I have one file which is in size around 20 MB , wanted to split up into four files of each size of 5 MB.

ABCD_XYZ_20130302223203.xml.

Requirement is that to write script which should do as : first three file should be of size 5 MB each, the fourth one content should be in the last file whatever it may be less or more than 5 MB.

Appreciate your help in this.
# 2  
Old 04-16-2014
Code:
split -b 5M ABCD_XYZ_20130302223203.xml

# 3  
Old 04-16-2014
ajju,

you want create only four files ?
I mean if file size is 23 MB, do you want four files (last one should contain everything after 15 MB) or five files
Code:
file1=5MB
file2=5MB
file3=5MB
file4=5MB
file5=3MB

oR
Code:
file1=5MB
file2=5MB
file3=5MB
file4=8MB

# 4  
Old 04-16-2014
Both the solutions are expected...

primarily four files like with the same naming conventions as original appending 1,2,3 as suffix......

File1=5MB
file2= 5MB
file3= 5Mb
file4=8MB
# 5  
Old 04-16-2014
Code:
filename="ABCD_XYZ_20130302223203.xml"
split -b 5M -a2 -d $filename "${filename}_"

# 6  
Old 04-16-2014
Pravin,
Thanks for your input, but Its AIX -d not workingSmilie

any other options?

---------- Post updated at 05:54 AM ---------- Previous update was at 03:17 AM ----------

cant we do it this thing by awk?
# 7  
Old 04-16-2014
You could do this with awk, but it really isn't the right tool for this job.

The following seems to do what you want and should work on AIX:
Code:
#!/bin/ksh
file="ABCD_XYZ_20130302223203.xml"
base="${file%.*}_part"
ext=".${file##*.}"
m5=$((5*1024*1024))
for i in 1 2 3 4
do	if [ $i -gt 1 ]
	then	skip="skip=$((i - 1))"
	else	skip=""
	fi
	if [ $i -lt 4 ]
	then	count="count=1"
	else	count=""
	fi
	of="$base$i$ext"
	printf "Creating %s\n" "$of"
	dd if="$file" of="$of" bs=$m5 $count $skip
done

With m5 set to 5 instead of 5242880 and a much smaller file for testing, you might get output something like:
Code:
Creating ABCD_XYZ_20130302223203_part1.xml
1+0 records in
1+0 records out
5 bytes transferred in 0.000044 secs (113360 bytes/sec)
Creating ABCD_XYZ_20130302223203_part2.xml
1+0 records in
1+0 records out
5 bytes transferred in 0.000036 secs (138884 bytes/sec)
Creating ABCD_XYZ_20130302223203_part3.xml
1+0 records in
1+0 records out
5 bytes transferred in 0.000027 secs (185589 bytes/sec)
Creating ABCD_XYZ_20130302223203_part4.xml
68+1 records in
68+1 records out
341 bytes transferred in 0.000303 secs (1125301 bytes/sec)

leaving you with the files:
Code:
-rwxr-xr-x  2 dwc  staff  356 Apr 16 17:51 ABCD_XYZ_20130302223203.xml
-rw-r--r--  1 dwc  staff    5 Apr 16 18:00 ABCD_XYZ_20130302223203_part1.xml
-rw-r--r--  1 dwc  staff    5 Apr 16 18:00 ABCD_XYZ_20130302223203_part2.xml
-rw-r--r--  1 dwc  staff    5 Apr 16 18:00 ABCD_XYZ_20130302223203_part3.xml
-rw-r--r--  1 dwc  staff  341 Apr 16 18:00 ABCD_XYZ_20130302223203_part4.xml

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting the XML file into three different files

Hello Shell Guru's I have a requirement to split the source xml file into three different text file. And i need your valuable suggestion to finish this. Here is my source xml snippet, here i am using only one entry of <jms-system-resource>. There may be multiple entries in the source file. ... (5 Replies)
Discussion started by: Siv51427882
5 Replies

2. UNIX for Beginners Questions & Answers

Splitting a file based on a pattern

Hi All, I am having a problem. I tried to extract the chunk of data and tried to fix I am not able to. Any help please Basically I need to remove the for , values after K, this is how it is now A,, B, C,C, D,D, 12/04/10,12/04/10, K,1,1,1,1,0,3.0, K,1,1,1,2,0,4.0,... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

3. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

4. Shell Programming and Scripting

Splitting file based on pattern and first character

I have a file as below pema.txt s2dhshfu dshfkdjh dshfd rjhfjhflhflhvflxhvlxhvx vlvhx sfjhldhfdjhldjhjhjdhjhjxhjhxjxh sjfdhdhfldhlghldhflhflhfhldfhlsh rjsdjh#error occured# skjfhhfdkhfkdhbvfkdhvkjhfvkhf sjkdfhdjfh#error occured# my requirement is to create 3 files frm the... (8 Replies)
Discussion started by: pema.yozer
8 Replies

5. Shell Programming and Scripting

Problem with splitting large file based on pattern

Hi Experts, I have to split huge file based on the pattern to create smaller files. The pattern which is expected in the file is: Master..... First... second.... second... third.. third... Master... First.. second... third... Master... First... second.. second.. second..... (2 Replies)
Discussion started by: saisanthi
2 Replies

6. Shell Programming and Scripting

Splitting single file into n files

Hi all, I am new to scripting and I have a requirement we have source file as HEADER 01.10.2010 14:32:37 NAYA TA0022 TA0000 20000001;20060612;99991231;K4;02;3 20000008;20080624;99991231;K4;02;3 20000026;19840724;99991231;KK;01;3 20000027;19840724;99991231;KK;01;3... (6 Replies)
Discussion started by: srk409
6 Replies

7. Shell Programming and Scripting

Data Splitting into two files from one file

I have a file as: I/P File: Ground Car 2009 Lib 2008 Lib 2003 Ground Car 2009 Ground Car 2003 Car 2005 Car 2003 Car 2005 Sita 2900 2006 Car 2007 I have to split the file into two: - one for names and second for years. O/p1 (Names): Ground Car (3 Replies)
Discussion started by: karumudi7
3 Replies

8. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

9. Shell Programming and Scripting

Splitting file into 2 files ?

Hi extending to one of my previous posted query .... I am using nawk -v invar1="$aa" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_NETWORKID.txt")}' spfile.txt to get 2 different files based on split condition i.e. "|" Similar to invar1 variable in nawk I also need one more variable... (18 Replies)
Discussion started by: shekharjchandra
18 Replies

10. Shell Programming and Scripting

Splitting files from one file

Hi, I have an input file like: 111 abcdefgh asdfghjk dfghjkl 222 aaaaaaa bbbbbb 333 djfhfgjktitjhgfkg 444 djdhfjkhfjkghjkfg hsbfjksdbhjkgherjklg fjkhfjklsahjgh fkrjkgnj I want to read this input file and make separate output files with the header as numric value like "111"... (9 Replies)
Discussion started by: saltysumi
9 Replies
Login or Register to Ask a Question