Splitting the Huge file into several files...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting the Huge file into several files...
# 1  
Old 03-16-2010
Splitting the Huge file into several files...

Hi

I have to write a script to split the huge file into several pieces. The file columns is | pipe delimited. The data sample is as:

6625060|1420215|07308806|N|20100120|5572477081|+0002.79|+0000.00|0004|0001|......
6625060|1445972|10476049|N|20100120|5572477081|+0003.29|+0000.00|0004|0002|......
6625060|1001309|10491766|N|20100120|5572459582|+0002.59|+0000.00|0006|0001|......
6625060|1002119|10303228|N|20100120|5572477081|+0002.49|+0000.00|0004|0001|......

My requirement is to split this file based on the date in 5th column. The condition is based on this date and to be splitted by month. So, the number of files would be 12 (a file for each month).

Please help me to write a Unix script to split several files.

Thanks
LakTeja
# 2  
Old 03-16-2010
you can use awk:
Code:
awk -F'|' { file=sprintf("%s_%s", FILENAME, substr($5,5,2)); print $0 >> file}' inputfile

will produce files names inputfile_01 inputfile_02 ... inputfile_12 if there is data for all months in inputfile.
# 3  
Old 03-16-2010
Code:
awk -F"|" ' { print > substr($5,5,2) } ' file

# 4  
Old 03-16-2010
Hi, lakteja:

Welcome to the forums. The following AWK one-liner will do what you ask. One file per month based on the date in the fifth pipe-delimited column. The filename will be in the form YYYYMM:
Code:
awk -F\| '{print > substr($5,1,6)}' file

Should you prefer the files to be named with just the month, MM:
Code:
awk -F\| '{print > substr($5,5,2)}' file

Regards,
Alister

---------- Post updated at 11:13 AM ---------- Previous update was at 10:55 AM ----------

A shell version which generates YYYYMM filenames:
Code:
#!/bin/sh

file=$1
IFS=\|
while read -r line; do
    set -- $line
    echo "$line" >> ${5%??}
done < "$file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Split a huge 7 GB File Based on Pattern into 4 files

Hi, I have a Huge 7 GB file which has around 1 million records, i want to split this file into 4 files to contain around 250k messages each. Please help me as Split command cannot work here as it might miss tags.. Format of the file is as below <!--###### ###### START-->... (6 Replies)
Discussion started by: KishM
6 Replies

2. UNIX for Dummies Questions & Answers

File comparison of huge files

Hi all, I hope you are well. I am very happy to see your contribution. I am eager to become part of it. I have the following question. I have two huge files to compare (almost 3GB each). The files are simulation outputs. The format of the files are as below For clear picture, please see... (9 Replies)
Discussion started by: kaaliakahn
9 Replies

3. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 Replies

4. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

5. Shell Programming and Scripting

Help- counting delimiter in a huge file and split data into 2 files

I’m new to Linux script and not sure how to filter out bad records from huge flat files (over 1.3GB each). The delimiter is a semi colon “;” Here is the sample of 5 lines in the file: Name1;phone1;address1;city1;state1;zipcode1 Name2;phone2;address2;city2;state2;zipcode2;comment... (7 Replies)
Discussion started by: lv99
7 Replies

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

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

8. Shell Programming and Scripting

Help on splitting this huge file

Hi , i have files coming in my system which are very huge in MB and GBs, all these files are in a single line, there is no newline character. I need to get only last 700 bytes of these files, of this i am splitting the files by "split -b 700 filename" but this gives all the splitted... (2 Replies)
Discussion started by: Prateek007
2 Replies

9. Shell Programming and Scripting

splitting huge xml into multiple files

hi all i have a some huge html files (500MB to 1GB). Each file has multiple <html></html> tags <html> ................. .................... .................... </html> <html> ................. .................... .................... </html> <html> .................... (5 Replies)
Discussion started by: uttamhoode
5 Replies

10. Shell Programming and Scripting

Splitting huge XML Files into fixsized wellformed parts

Hi, I need to split xml-files with sizes greater than 2 gb into smaler chunks. As I dont want to end up with billions of files, I want those splitted files to have configurable sizes like 250 MB. Each file should be well formed having an exact copy of the header (and footer as the closing of the... (0 Replies)
Discussion started by: Malapha
0 Replies
Login or Register to Ask a Question