File splitting and grouping using unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File splitting and grouping using unix script
# 1  
Old 04-20-2010
File splitting and grouping using unix script

Hello All,

I have a small problem with file group/splitting and I am trying to get the best way to perform this in unix. I am trying with awk but need some suggestion what would be the best and fastest way to-do it.

Here is the problem. I have a fixed length file with filled with product data, I need to split each product in different file grouped.

Example main content file: data.txt

> cat data.txt
123456 Hello World 1
234455 Hello World 2
123456 Hello World 3
234455 Hello World 4
999922 Hello World 5
123456 Hello World 6
999922 Hello World 7

Layout
first 6 characters production identifier, rest of the data belong to the same product.

When I split, I need files name with product name with all product data grouped in one file.

Result after split. I will have 3 files namely 123456, 234455 and 999922.

123456.txt
123456 Hello World 1
123456 Hello World 3
123456 Hello World 6

234455.txt
234455 Hello World 2
234455 Hello World 4

999922.txt
999922 Hello World 5
999922 Hello World 7

If you can provide any input that will be awesome. Smilie

Thanks a lot.
_nandhan_
# 2  
Old 04-20-2010
A sh solution:
Code:
while IFS= read -r l; do
    echo "$l" >> ${l%% *}.txt
done < data.txt

Regards and welcome to the forum,
Alister

Last edited by alister; 04-20-2010 at 10:33 PM.. Reason: forgot .txt. Thanks, Ygor
# 3  
Old 04-20-2010
Using awk...
Code:
awk '{print $0 > $1 ".txt"}' data.txt

# 4  
Old 04-20-2010
If you hit a file descriptor limit, you can try:
Code:
awk '{f=$1".txt"; print >> f; close(f)}' data.txt


Last edited by alister; 04-20-2010 at 11:49 PM..
# 5  
Old 04-20-2010
Thanks for quick reply... It works. I have a huge file (500mg) going to try now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File splitting script help

Hi All, I have file in my system with below data PP1234512345671234567CABC PP1234512345671234567BABC PP1234512345671234567BABC PP1234512345671234567CABC PP1234512345671234567DABC PP1234512345671234567EABC PP1234512345671234567DABC PP1234512345671234567EABC... (10 Replies)
Discussion started by: ibrar Ahmad
10 Replies

2. Shell Programming and Scripting

UNIX grouping

Hi guys, I am a complete newbie to unix and have been tasked with creating a script to group the following data (file) by hourly slots so that I can count the transactions completed within the peak hour. I am not sure how to group data like this in unix. Can anyone please help? Here is an... (1 Reply)
Discussion started by: MrMidas
1 Replies

3. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

4. UNIX for Dummies Questions & Answers

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (6 Replies)
Discussion started by: mkandula1983
6 Replies

5. Shell Programming and Scripting

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (1 Reply)
Discussion started by: mkandula1983
1 Replies

6. Shell Programming and Scripting

Conditional File Splitting in Unix

Hello Guys, I am new to Unix and i got one requirement where I need to split my file into maximum 5 files on below conditions, if they are splitted into less than 5 then also we are fine. Columns D value should appear in a single file only and shouldn't continue in other files. ... (1 Reply)
Discussion started by: Rizzu155
1 Replies

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

8. Shell Programming and Scripting

script for splitting file

Can anyone help me in giving a script for the below scenario I have file from the source increamenting in size... I require to write a script witch will move the data to the new file once the file reaches 50MB of size . This needs If the first file reaches 50MB then my script has to generate... (3 Replies)
Discussion started by: Sudhakishore.P
3 Replies

9. Shell Programming and Scripting

grouping records in a file in unix

I have file like this d123, rahim, 140 d123, rahul, 440 d123, begum, 340 d234, bajaj, 755 d234, gajal, 657 I want to group this file like this d123, rahim, 140 rahul, 440 begum, 340 d234, bajaj, 755 gajal, 657 can any one help me on this thanks in advance (8 Replies)
Discussion started by: trichyselva
8 Replies

10. Shell Programming and Scripting

splitting a pipe delimited file in unix

Could one of you shad some light on this: I need to split the file by determining the record count and than splitting it up into 4 files. Please note, this is not a fixed record length but rather a "|" delimited file. I am not sure as how to handle reminder/offset for the 4th file. For... (4 Replies)
Discussion started by: ddedic
4 Replies
Login or Register to Ask a Question