Splitting a file based on some condition and naming them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a file based on some condition and naming them
# 1  
Old 12-07-2005
Data Splitting a file based on some condition and naming them

I have a file given below. I want to split the file where ever I came across
***(instead you can put ### symbols in the file) . Also I need to name the file by extracting the report name from the first line which is in bold(eg:RPT507A) concatinated with DD(day on which the file runs). Can someone help me out in this......................

Thanks in advance
srivsn





==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 1
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:12 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________

==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 2
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:13 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________

==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 3
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:13 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________

==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 4
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:14 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________

==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 5
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:15 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________


==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 6
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:16 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________


==================================================================================================== ================================
RPT507A BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 7
Bath and Body Works Disputed Transactions Report Job: 65
Wed 01:55:16 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================
STORE CARD NUMBER DATE TIME AMOUNT MERCHANT INVOICE NO SEQ# CODE Reason
__________ ___________________ __________ ________ __________________ ________ __________ ______ ____ ___________________



* * * END OF REPORT * * *
### Job 65 (RPT507A BBW ): 390 lines, 7 pages, 10/26/2005 01:55:16
==================================================================================================== ================================
RPT507B BBW \CDL01 prd1 STORED VALUE SYSTEMS Page 1
Bath and Body Works Disputed Transactions Report Job: 86
Wed 01:55:17 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================



* * * END OF REPORT * * *
### Job 86 (RPT507B BBW ): 25 lines, 1 pages, 10/26/2005 01:55:17
==================================================================================================== ================================
RPT507B BBWCC \CDL01 prd1 STORED VALUE SYSTEMS Page 1
White Barn Candle Company Disputed Transactions Report Job: 87
Wed 01:55:17 Settlement file dated 10/25/2005 Oct 26 2005
==================================================================================================== ================================



* * * END OF REPORT * * *
# 2  
Old 12-07-2005
I'd use perl for something that involved.Something like:-

#!/usr/bin/perl

# Use Perls localtime function if you want, but this is plainer
$date=`date '+%d_%m'`;
chomp($date);

open (INPUTFILE, "<./report_file.rep");
(@file)=<INPUTFILE>;
close INPUTFILE;

@report_names=grep (/RPT/,@file);
@reports=split(/\*\*\*.*?\*\*\*/, join("",@file));


$count=0;
foreach $report_name(@report_names) {
$report_name=~s/(\w+)\W.*/\1/;
chomp($report_name);
$filename="${report_name}_${date}";

open (FILE, ">./$filename");
print FILE "$reports[$count]\n";
close FILE;

$count++;
}


You'll probably need to tweak the 'split' function a bit so each file contains exactly what you want, but that (roughly) should do it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Beginners Questions & Answers

Consternation of multiple file names based on naming pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

3. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

4. Shell Programming and Scripting

Splitting file based on column values

Hi all, I have a file (say file.txt) which contains comma-separated rows. Each row has seven columns. Only column 4 or 5 (not both) can have empty values like "" in each line. Sample lines So, now i want all the rows that have column 4 as "" go in file1.txt and all the rows that have column... (8 Replies)
Discussion started by: jakSun8
8 Replies

5. Shell Programming and Scripting

Splitting a file based on context.

I have file as shown below. Would like to split the file based on the context of data. Like, split the content between "---- XXX Info ----" and " ---- YYY Info ----" to a file. When I try using below command, 2nd file contains all the info starting after first "---- YYYY Info ----" instance.... (8 Replies)
Discussion started by: webkid
8 Replies

6. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

7. UNIX for Dummies Questions & Answers

Splitting a file based on first 8 chars

I have an input file of this format <Date><other data> For example, 20081213aaaaaaaaa 20081213bbbbbbbbb 20081220ccccccccc 20081220ddddddddd 20081220eeeeeeeee 20081227ffffffffffffff The first 8 chars are date in YYYYMMDD formT. I need to split this file into n files where n is the... (9 Replies)
Discussion started by: paruthiveeran
9 Replies

8. Shell Programming and Scripting

Dynamic naming based on file content

I was just thinking if there is a way where i can dynamically rename files based on the actual file content. I have a load of pdf's which have been named wrongly. (We normally put date first, then brief description, then title) So can a script be written wherin, it pulls out the date and title... (2 Replies)
Discussion started by: deaddevil
2 Replies

9. Shell Programming and Scripting

Splitting a file based on two patterns

Hi there, I've an input file as follows: *START 1001 a1 1002 a2 1003 a3 1004 a4 *END *START 1001 b1 1002 b2 1004 b4 *END *START 1001 c1 1004 c4 *END (6 Replies)
Discussion started by: kbirde
6 Replies

10. Shell Programming and Scripting

Splitting the file based on logic

Hello I have a requirement where i need to split the Input fixed width file which contains multiple invoices into multiple files with 2 invoices per file. Each invoice can be identified by its first line's second character which is "H" and sixth character is " " space and the invoice would... (10 Replies)
Discussion started by: dsdev_123
10 Replies
Login or Register to Ask a Question