Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Split files into smaller ones with 1000 hierarchies in a single file. Post 302921122 by Akshay Hegde on Wednesday 15th of October 2014 04:58:58 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy