Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chopping the file 1

Status
Not open for further replies.

maxcrook

Programmer
Jan 25, 2001
210
GB
A challenge !

I have a continuous stream of data split like this:

BILLSTART
ACCOUNTNAME nnnnn
PRODUCT nnn

Then more stream like this

122222222 Call1,,,,,0122345455,call5 etc etc

Then

BILLEND

I need to be able to cut one account out from the stream to create one file ! How can I do this ? I have tried using split but that only cuts the file down to contain the amount of lines specified in split -n

How can I extract one account into a file baring in mind that each accounts bill starts

BILLSTART
and finishes
BILLEND

I want to be able to specify the account number to extract but the ACCOUNT_NO field is located on the 3rd line !

Anyone know what I am trying to get at ?
 
Max,

Tasks like this are fairly easy in Perl, do you have it? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Funny you should say that I have just started to learn Perl for this reason - but obviously I am not even close to manipulating data like this as I have only been studying it for a week !

 
Ok -- Try something like this then.

[tt]
#!/path/to/perl/executable

# get the account name to print from command line
$acc_name = shift;
LINE: while(<>){
[tab]unless ($FoundAccount) {
[tab][tab]next LINE unless /^ACCOUNTNAME $acc_name$/;
[tab][tab]$FoundAccount = 1;
[tab]}
[tab]exit 0 if /^BILLEND/;
[tab]print;
}
[/tt]


Put the above in a file called print_acc_bill.pl, change the path to perl line to match where perl is found on your system (on mine it's /usr/bin/perl for instance) and then set it to executable with the command:
[tt]
chmod +x print_acc_bill.pl
[/tt]

You can then print the account data for account name 12345 from the data file ACC_DATA with the following command:

./print_acc_bill.pl 12345 ACC_DATA
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Michael thanks for this but I cannot get the script to run !

I have amended the script as necessary to incorporate the actual paths for the file that I want to extract the data from. Any ideas ?

Thanks again - I will keep trying.
 
Michael

No got it to run but got the following error:

Search pattern not terminated in file ./print_acc_bill.pl at line 5, next char ^?
syntax error in file ./print_acc_bill.pl at line 6, next token &quot;$acc_no&quot;
Execution of ./print_acc_bill.pl aborted due to compilation errors.

Any thoughts ?
 
Max -- could you post your script please? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Michael here's the script - I have only amended it slighty to incorporate the actual files I have - also here is what I type on the command line:

./print_bill_acc.pl nnnnnn exp_8_136_1

(where nnnnnn is the account number)

#!/usr/lpp/ssp/perl/bin/perl
$acc_no = shift;LINE: while(<>) {
unless ($FoundAccount) {
next LINE unless /^ACCOUNTNO
$acc_no$/;
$FoundAccount = 1;
}
exit 0 if /^BILLEND/;
print;
}


Here is what the bill stream looks like:

BILLSTART_1
BILLVERSION n.n
BILLSTYLE n
BILLTYPE n
BILLTEMPLATE n
ACCCURRENCYCODE GBP
PAYMETHODID n
FORMATREQ nnnnnn/nnnn
ADDRESSNAME
BUSINESSNAME NODDY HOLDER
ADDRESS1 1-2 TOYTOWN
ADDRESS2 CAKVILLE
ZIPCODE C3 P0
COUNTRY MADE UP
CUSTOMERREF nnn
CUSTOMERTYPE
ACCOUNTNO nnnnnn

There is more until you hit the field BILLEND.

I have changed all the details to reflect a dummy account.
 
Max,

The two lines:
[tt]
next LINE unless /^ACCOUNTNO
$acc_no$/;
[/tt]

should be one line:
[tt]
next LINE unless /^ACCOUNTNO $acc_no$/;
[/tt]

Search patterns, unless you use the /x thingy - don't worry about that now, have to be on the one line.
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Excellent nearly there !

Just a couple of things really......

The script works fine but it cuts the first 16 lines off the top of the bill before you reach the ACCOUNTNO

These are:
BILLSTART_1
BILLVERSION n.n
BILLSTYLE n
BILLTYPE n
BILLTEMPLATE n
ACCCURRENCYCODE GBP
PAYMETHODID n
FORMATREQ nnnnnn/nnnn
ADDRESSNAME
BUSINESSNAME NODDY HOLDER
ADDRESS1 1-2 TOYTOWN
ADDRESS2 CAKVILLE
ZIPCODE C3 P0
COUNTRY MADE UP
CUSTOMERREF nnn
CUSTOMERTYPE

Plus it also cuts off the end field - called BILLEND -which I need in the file.

Sorry for being a pain but as I have stated before I do not know much about Perl - sorry. You have been a great help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top