Sponsored Content
Top Forums Shell Programming and Scripting divide a single file with different Weboffercodes into different files with each of o Post 302180411 by aigles on Monday 31st of March 2008 05:48:51 AM
Old 03-31-2008
A possible solution :
Code:
$ cat web.awk
#!/usr/bin/awk -f

BEGIN {
   ErrorCode = "invalid";
}

{
   #Item Code | Account Number | Card Number | Source code | WebOfferCode
   #12digits | 10 Digits | 15 digits | 10 letters | 20 letters

   ItemCode      = substr($0,  1, 12);
   AccountNumber = substr($0, 13, 10);
   CardNumber    = substr($0, 23, 15);
   SourceCode    = substr($0, 38, 10);
   WebOfferCode  = substr($0, 48, 20);


   cn  = CardNumber  ; gsub(/[0 ]/, "", cn  );
   woc = WebOfferCode; gsub(/[0 ]/, "", woc );
   if (cn == "" || woc == "")
      OutputFile = "woc_" ErrorCode ".dat"
   else
      OutputFile = "woc_" WebOfferCode ".dat"
   print $0 >> OutputFile;

   printf("%-28s : %s|%s|%s|%s|%s\n", OutputFile, ItemCode, AccountNumber, CardNumber, SourceCode, WebOfferCode);
}

$ cat web.dat
3412222240171M41006422373273564021006SDFGHJRSGJBBBBBBBBBBBBBBBBBBBB
0009546640171M41006400373273564531345TTTTPAPAP
0009546640171M41006400373273564531345TTTTPAPAP00000000000000000000
0009546640171M41006400               TTTTPAPAPBBBBBBBBBBBBBBBBBBBB
0009546640171M41006400000000000000000TTTTPAPAPBBBBBBBBBBBBBBBBBBBB
0009546640171M41006400373273564531345TTTTPAPAPBBBBBBBBBBBBBBBBBBBB
0009546640171M41006433373273564021006YYYYPAPAPHHHHHHHHHHHHHHHHHH
3412222240171M41006455373273564021116PAPAPAPAPBBBBBBBBBBBBBBBBBBBB
0009546640171M41006400373273564531345UUUUPAPAPHHHHHHHHHHHHHHHHHH
0009546640171M41006477373273564021006SDFGHJRSGJBBBBBBBBBBBBBBBBBBBBB
$ web.awk web.dat
woc_BBBBBBBBBBBBBBBBBBBB.dat : 341222224017|1M41006422|373273564021006|SDFGHJRSGJ|BBBBBBBBBBBBBBBBBBBB
woc_invalid.dat              : 000954664017|1M41006400|373273564531345|TTTTPAPAP |
woc_invalid.dat              : 000954664017|1M41006400|373273564531345|TTTTPAPAP0|0000000000000000000
woc_invalid.dat              : 000954664017|1M41006400|               |TTTTPAPAPB|BBBBBBBBBBBBBBBBBBB
woc_invalid.dat              : 000954664017|1M41006400|000000000000000|TTTTPAPAPB|BBBBBBBBBBBBBBBBBBB
woc_BBBBBBBBBBBBBBBBBBB.dat  : 000954664017|1M41006400|373273564531345|TTTTPAPAPB|BBBBBBBBBBBBBBBBBBB
woc_HHHHHHHHHHHHHHHHH.dat    : 000954664017|1M41006433|373273564021006|YYYYPAPAPH|HHHHHHHHHHHHHHHHH
woc_BBBBBBBBBBBBBBBBBBB.dat  : 341222224017|1M41006455|373273564021116|PAPAPAPAPB|BBBBBBBBBBBBBBBBBBB
woc_HHHHHHHHHHHHHHHHH.dat    : 000954664017|1M41006400|373273564531345|UUUUPAPAPH|HHHHHHHHHHHHHHHHH
woc_BBBBBBBBBBBBBBBBBBBB.dat : 000954664017|1M41006477|373273564021006|SDFGHJRSGJ|BBBBBBBBBBBBBBBBBBBB
$ ls -1 woc_*.dat
woc_BBBBBBBBBBBBBBBBBBB.dat
woc_BBBBBBBBBBBBBBBBBBBB.dat
woc_HHHHHHHHHHHHHHHHH.dat
woc_invalid.dat
$

With gawk, you can also do :
Code:
$ cat web2.awk
#!/usr/bin/gawk -f

BEGIN {
   FIELDWIDTHS = "12 10 15 10 20";
   ErrorCode = "invalid";
}

{
   #Item Code | Account Number | Card Number | Source code | WebOfferCode
   #12digits | 10 Digits | 15 digits | 10 letters | 20 letters

   ItemCode      = $1;
   AccountNumber = $2;
   CardNumber    = $3;
   SourceCode    = $4;
   WebOfferCode  = $5;


   cn  = CardNumber  ; gsub(/[0 ]/, "", cn  );
   woc = WebOfferCode; gsub(/[0 ]/, "", woc );
   if (cn == "" || woc == "")
      OutputFile = "woc_" ErrorCode ".dat"
   else
      OutputFile = "woc_" WebOfferCode ".dat"
   print $0 >> OutputFile;

   # DebuG
   printf("%-28s : %s|%s|%s|%s|%s\n", OutputFile, ItemCode, AccountNumber, CardNumber, SourceCode, WebOfferCode);
}
$

Jean-Pierre.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to divide single large log file into multiple files.

Can you please help me with writing script for following purpose. I have to divide single large web access log file into multiple log files based on dates inside the log file. For example: if data is logged in the access file for jan-10-08 , jan-11-08 , Jan-12-08 then make small log file... (1 Reply)
Discussion started by: kamleshm
1 Replies

2. Shell Programming and Scripting

Match two files and divide a field !

Hello, I have two files that have the date field as a common. I request your help with some script that divide the value of the file1 by the value in the file2 only when the field date are the same between both files and create a new text file. This is a sample of the files file1... (1 Reply)
Discussion started by: csierra
1 Replies

3. Shell Programming and Scripting

Divide large data files into smaller files

Hello everyone! I have 2 types of files in the following format: 1) *.fa >1234 ...some text... >2345 ...some text... >3456 ...some text... . . . . 2) *.info >1234 (7 Replies)
Discussion started by: ad23
7 Replies

4. Shell Programming and Scripting

Match two files and divide fields

I have two files that have the date field in common. I request your help with some script that divide each field value from file1 by the correspond field value of the file2 only when the field date is equal in both files. Thanks in advance ! This is a sample of the files file 1 12/16/2010,... (2 Replies)
Discussion started by: csierra
2 Replies

5. UNIX for Dummies Questions & Answers

divide the file into multiple files based on the city name

Hi, I have a file abc.dat. It contains the fileds of empid, empname, empcity. each city contains 10 records. i want to create the city file and pass the same city records into the file. I don't know the city names. In unix using awk command how can we do? abc.dat: 1 john delhi 2... (2 Replies)
Discussion started by: raghukreddy.ab
2 Replies

6. Shell Programming and Scripting

Divide data into separate files

frnds: i want to divide data on the behalf of dotted line and redirectd into new files ) ------------------------- M-GET CONFIRMATION ( ------------------------- M-GET CONFIRMATION ( INVOKE IDENTIFIER final data shuld be into 3 files ...... (6 Replies)
Discussion started by: dodasajan
6 Replies

7. Shell Programming and Scripting

Divide data with specific column values into separate files

hello! i need a little help from you :) ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example. ... (3 Replies)
Discussion started by: chanclitas
3 Replies

8. Shell Programming and Scripting

Divide an EBCDIC files into multiple files based on value at 45-46 bytes

Hi All, I do have an EBCDIC file sent from the z/os , this file has records with different record types in it, the type of record is identified by bytes 45-46 like value 12 has employee record value 14 has salaray record and etc.... we do now want to split the big ebcdic file into multiple... (3 Replies)
Discussion started by: okkadu
3 Replies

9. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

10. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies
All times are GMT -4. The time now is 02:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy