Split files into smaller ones with 1000 hierarchies in a single file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Split files into smaller ones with 1000 hierarchies in a single file.
# 1  
Old 10-15-2014
Split files into smaller ones with 1000 hierarchies in a single file.

Code:
input file:
AD,00,--,---,---,---,---,---,---,--,--,---
   AM,000,---,---,---,---,---,---
   AR, ,---,--,---,---
   AA,---,---,---,---
   AT,---
   AU,---,---,---
   AS,---,---
   AP,---,---,---
   AI,---
AD,00,---,---,---, ,---,---,---,---,---,---
   AM,000,---,---,---
   AR, ,---,---,---
   AS,---,---
AD,01,---,---,---,---,---,---,---,---,---
   AA,001,---,---
   AU,---,---,---
AD,00,---,---,---,---,---,---,---,---,---,---
   AP,---,---,---,---,---,---,---
AD,01,---,---,---,---,---
   AS,---,---,---,---
   AI,---,---

Basics of file:
Record starting with 'AD' is the parent record and all the sub records starting with other sequences are its properties (of parent 'AD' record).
"," is the delimiter for the file.
There may be as lowest as 1 property or as highest as 8 properties for each hierarchy ('AD' record).
There may be millions of such hierarchies...

Requirement:
So need to split the master file into several sub files such that each sub file contain 1000 hierarchies each (1000 'AD' records along with their properties records)

I tried using code: awk '/AD/{x="F"++i;}{print > x;}' test.txt
but i see the hierarchies are divided with 1 hierarchy in each file.
Can someone help me to divide file such that there are 1000 hierarchies in each file.
Also i notice that after file split the hierarchy records are coming in 1 single line rather than multiple lines as like in source file.
Could someone please help me on the same.

Attached is the sample input file.
# 2  
Old 10-15-2014
Try

Code:
awk '/AD/{if(++f>h)f=1; if(f==1){close(file);file=sprintf("Output_%04d.txt",++s) }}{print >file}' h='2' file

change variable h highlighted
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-15-2014
Hi Akshay,

Thank you for your answer...It works...
But as i said i see the whole output is coming in 1 single line...
Can u help me in getting the output in separate lines like in similar format of input file..
Code:
AD,000,--,--,---
        AI,--,--,--
        AU,---,---,---
AD,00,--, ,---
        AS,--,--,--
        AT,--,--


Last edited by kcdg859; 10-15-2014 at 06:10 AM.. Reason: modified the reqd. layout
# 4  
Old 10-15-2014
Quote:
Originally Posted by kcdg859
Hi Akshay,

Thank you for your answer...It works...
But as i said i see the whole output is coming in 1 single line...
Can u help me in getting the output in separate lines like in similar format of input file..
AD,000,--,--,---
AI,--,--,--
AU,---,---,---
AD,00,--, ,---
AS,--,--,--
AT,--,--
I didn't understand , please show expected output using codetags.

---------- Post updated at 03:28 PM ---------- Previous update was at 03:17 PM ----------

I got output like this when h=2

Input
Code:
[akshay@nio tmp]$ cat infile
AD,00,--,---,---,---,---,---,---,--,--,---
   AM,000,---,---,---,---,---,---
   AR, ,---,--,---,---
   AA,---,---,---,---
   AT,---
   AU,---,---,---
   AS,---,---
   AP,---,---,---
   AI,---
AD,00,---,---,---, ,---,---,---,---,---,---
   AM,000,---,---,---
   AR, ,---,---,---
   AS,---,---
AD,01,---,---,---,---,---,---,---,---,---
   AA,001,---,---
   AU,---,---,---
AD,00,---,---,---,---,---,---,---,---,---,---
   AP,---,---,---,---,---,---,---
AD,01,---,---,---,---,---
   AS,---,---,---,---
   AI,---,---

Code executed
Code:
[akshay@nio tmp]$ awk '/AD/{if(++f>h)f=1; if(f==1){close(file);file=sprintf("Output_%04d.txt",++s) }}{print >file}' h='2' infile

Output files
Code:
[akshay@nio tmp]$ ls *.txt -1
Output_0001.txt
Output_0002.txt
Output_0003.txt


Contents of each file

Code:
[akshay@nio tmp]$ cat Output_0001.txt 
AD,00,--,---,---,---,---,---,---,--,--,---
   AM,000,---,---,---,---,---,---
   AR, ,---,--,---,---
   AA,---,---,---,---
   AT,---
   AU,---,---,---
   AS,---,---
   AP,---,---,---
   AI,---
AD,00,---,---,---, ,---,---,---,---,---,---
   AM,000,---,---,---
   AR, ,---,---,---
   AS,---,---

Code:
[akshay@nio tmp]$ cat Output_0002.txt 
AD,01,---,---,---,---,---,---,---,---,---
   AA,001,---,---
   AU,---,---,---
AD,00,---,---,---,---,---,---,---,---,---,---
   AP,---,---,---,---,---,---,---

Code:
[akshay@nio tmp]$ cat Output_0003.txt 
AD,01,---,---,---,---,---
   AS,---,---,---,---
   AI,---,---

Please let me know if I misunderstood your requirement

Please do post desired output for given sample when hierarchies = 2

Thanks
# 5  
Old 10-15-2014
Your input file is a DOS/windows file with \r\n line separators. Try converting it to *nix files first, then run above programs on it.
# 6  
Old 10-15-2014
Sorry Akshay...i got it wrong...ur code suits my requirement exactly...
I verified it...
But can i add a specialized header and footer info dynamically while splitting the files...for every individual files???
# 7  
Old 10-15-2014
Try this script

Code:
[akshay@nio tmp]$ cat split_data.awk
BEGIN{
	if( ARGC < 2 || !h )
	{
		
		getline t < "/proc/self/cmdline"; 
		split(t,T,"\0")
                error=1
		print "\n\tUsage     : awk -vh='< No of Hierarchies>' -f "T[3]" infile\n"
		print "\tExample   : awk -vh='2' -f "T[3]" mydata.txt\n"
		exit
		
	} 

	header="col1,col2,col3,etc"
	footer="foo,foobar,foobar,etc"
}
/AD/{
	if(++f>h)f=1; 
	if(f==1)
	{
		if(length(file))
		{
      		   print footer >file
                   close(file)
		}
		 
		  file=sprintf("Output_%04d.txt",++s)

		  print header >file 
	}
}
{
		print >file
}
END{
        if(!error)
	print footer >file
}

Usage
Code:
[akshay@nio tmp]$ awk -f split_data.awk

	Usage     : awk -vh='< No of Hierarchies>' -f split_data.awk infile

	Example   : awk -vh='2' -f split_data.awk mydata.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split large file into smaller files without disturbing the entry chunks

Dears, Need you help with the below file manipulation. I want to split the file into 8 smaller files but without cutting/disturbing the entries (meaning every small file should start with a entry and end with an empty line). It will be helpful if you can provide a one liner command for this... (12 Replies)
Discussion started by: Kamesh G
12 Replies

2. Shell Programming and Scripting

Split a single file into multiple files based on a value.

Hi All, I have the sales_data.csv file in the directory as below. SDDCCR; SOM ; MD6546474777 ;05-JAN-16 ABC ; KIRAN ; CB789 ;04-JAN-16 ABC ; RAMANA; KS566767477747 ;06-JAN-16 ABC ; KAMESH; A33535335 ;04-JAN-16 SDDCCR; DINESH; GD6674474747 ;08-JAN-16... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. UNIX for Dummies Questions & Answers

Split larger files into smaller ones with Column names

Hi, I have one large files of 100000 rows with header column. Eg: Emp Code, Emp Name 101,xxx 102,YYY 103,zzz ... ... I want to split the files into smaller files with only 30000 rows each..File 1,2 and 3 must have 30000 rows and file 4 must contain 10000 rows. But the column... (1 Reply)
Discussion started by: Nivas
1 Replies

4. UNIX for Dummies Questions & Answers

Split large file to smaller fastly

hi , I have a requirement input file: 1 1111111111111 108 1 1111111111111 109 1 1111111111111 109 1 1111111111111 110 1 1111111111111 111 1 1111111111111 111 1 1111111111111 111 1 1111111111111 112 1 1111111111111 112 1 1111111111111 112 The output should be, (19 Replies)
Discussion started by: mechvijays
19 Replies

5. Shell Programming and Scripting

Help needed - Split large file into smaller files based on pattern match

Help needed urgently please. I have a large file - a few hundred thousand lines. Sample CP START ACCOUNT 1234556 name 1 CP END ACCOUNT CP START ACCOUNT 2224444 name 1 CP END ACCOUNT CP START ACCOUNT 333344444 name 1 CP END ACCOUNT I need to split this file each time "CP START... (7 Replies)
Discussion started by: frustrated1
7 Replies

6. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

7. Shell Programming and Scripting

How to split a file into smaller files

Hi, I have a big text file with m columns and n rows. The format is like: STF123450001000200030004STF123450005000600070008STF123450009001000110012 STF234560345002208330154STF234590705620600070080STF234567804094562357688 STF356780001000200030004STF356780005000600070080STF356780800094562657687... (2 Replies)
Discussion started by: wintersnow2011
2 Replies

8. UNIX for Dummies Questions & Answers

Split single file into n number of files

Hi, I am new to unix. we have a requirement here to split a single file into multiples files based on the number of people available for processing. So i tried my hand at writing some code as below. #!/bin/bash var1=`wc -l $filename` var2=$var1/$splitno split -l $var2 $1 Please help me... (6 Replies)
Discussion started by: quirkguy
6 Replies

9. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

10. UNIX for Dummies Questions & Answers

split a single sql file into multiple files

Hi,I have a single sql file containing many create table ddl's.Example: CREATE TABLE sec_afs ( rpt_per_typ_c char(1) NOT NULL, rpt_per_typ_t varchar(20) NULL, LOCK ALLPAGES go EXEC sp_primarykey 'sec_afs', rpt_per_typ_c go GRANT SELECT ON sec_afs TO developer_read_only... (5 Replies)
Discussion started by: smarter_aries
5 Replies
Login or Register to Ask a Question