Computing.Net > Forums > Unix > File Split

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

File Split

Reply to Message Icon

Name: Fouad
Date: January 29, 2009 at 03:13:40 Pacific
OS: AIX 5.2
CPU/Ram: 4/8GB
Product: Ibm / P5
Subcategory: Configurations
Comment:

I am trying to split an EDI file which contents a lot off invoices in two files.
The end of invoice segments has two different strings (text):
1- “FTX Credit note retro active price adjustments” or
2- “FTX Self-billed invoices”

Also the result will be File1=> all Invoices with string Nr.1
File2=> all invoices segments with string Nr.2
The main file look like that:

//ODETT//INVGM
UNB 090114
MID 00003456 090114
SDT012837259
…..
……
FTX Credit note retro active price adjustments

/ODETT//INVGM
UNB 090114
MID 00001918 090114
SDT012837259
……
……
FTX Self-billed invoices

//ODETT//INVGM
UNB 090107
MID 00001222 090107
SDT012837259
……
……
FTX Self-billed invoices

//ODETT//INVGM
UNB 090115
MID 00004180 090115
SDT012837259
…….
…….
FTX Credit note retro active price adjustments

…..

…..
And so on.

Thanks in advance



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 31, 2009 at 22:45:57 Pacific
Reply:

This script contains two passes. The first pass is an awk script that determines whether an invoice belongs to File1, File2, or sent to the null device. (I'm using nawk so you might have to change to awk):

#!/bin/ksh

# two pass method.  Make a pass thru the data file and determine whether the
# invoice is "credit note" or "self-billed"
rm -f intradfile2 File1 File2

nawk '
{
if($1 == "FTX")
    # print once per invoice
   if($2 ~ /Credit*/)
      print "1"
   else
      if($2 ~ /Self-billed*/)
         print "2"
      else
         print "S" # skip

}' datafile > intradfile2


# Read the intermediate file into an array.  cycle thru the
# main file, keep a running count of the number of invoices and
# check the array to determine if the invoice belongs in File1 or File2 or /dev/null
nawk ' BEGIN { tot=0; cnt=0 # read the file into the array
   while ( getline line < "intradfile2" > 0 )
      {
      cnt++
      n[cnt]=line
      }
}
{

if($0 ~ /\/\/ODETT\/\/INVGM/)
   {
   tot++
   prcnt=n[tot]
   }

if(prcnt=="1")
  print $0 >> "File1"

if(prcnt=="2")
  print $0 >> "File2"

if(prcnt=="S")
  print $0 >> "/dev/null"

}' datafile


0

Response Number 2
Name: Fouad
Date: February 2, 2009 at 04:50:10 Pacific
Reply:

Many thanks Nail, the script works perfect.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: File Split

join binary file(split by split) www.computing.net/answers/unix/join-binary-filesplit-by-split/5556.html

How to enable faster downloads from www.computing.net/answers/unix/how-to-enable-faster-downloads-from/5948.html

Idendifying duplicate records www.computing.net/answers/unix/idendifying-duplicate-records/7141.html