1820882 Members
3603 Online
109628 Solutions
New Discussion юеВ

AWK Revisited Help

 
SOLVED
Go to solution
Darren Etheridge_2
Super Advisor

AWK Revisited Help

I have a script that sorts thru a source file and assigns our accounting information (fund, account). What I need help with is, the file works great with one exception... I need the EXCEPTIONS (values that do not match any of the Array values) written to a file.

#### Time / Date stamp file & Process

cat ws.datx >> wsdata.dat
echo "Parsing wsdata.dat" >> $LOGFL

cat /pei/peifas/ws/wsdata.dat |
awk 'BEGIN { FS=" "; x = 0; }
{
if ( x == 0 ){
b_month = substr($0,1,2)
b_day = substr($0,3,2)
b_fullyear = substr($0,5,4)
b_year = substr($0,7,2)
x = 1
} else {

value1 = substr($0, 1, 14)
gl_acct = 1234
#acct = substr($0, 4, 8)
damt = substr($0, 93, 13)
camt = substr($0, 105, 13)
desc = "W&S BATCH-" b_month""b_day""b_year
transamt = damt-camt
############# WS Case number & Clerk Equiv ##############

a["4111151000000"] = "411,2071130"
a["11310410110912"] = "411,1041010"
a["11310410110913"] = "411,1041010"
a["11310410110915"] = "411,1041010"

.
.
.

a["43034341010212"] = "411,2074300"
a["43034341010311"] = "411,2074300"

############################################################



if (a[value1]) {
split(a[value1],b,",")

fund=b[1]
acct=b[2]

printf("||%s|%s||||%s||%s|%s|N|%s/%s/%s|WSIMPORT|WS%s%s%s|||\n", fund, acct, gl_
acct, desc, transamt, b_month, b_day, b_fullyear, b_month, b_day, b_year)
} }
}' > ws.out


echo "Created ws.out file successfully " >> $LOGFL


(data file attached)

Your help is appreciated@!!!

Darren
10 REPLIES 10
Jean-Luc Oudart
Honored Contributor

Re: AWK Revisited Help

Darren,

have you tried the "join" command.
may answer your issues.

man join

Regards
Jean-Luc
fiat lux
Rodney Hills
Honored Contributor
Solution

Re: AWK Revisited Help

How about the following-

if (a[value1]) {
split(a[value1],b,",")

fund=b[1]
acct=b[2]

printf("||%s|%s||||%s||%s|%s|N|%s/%s/%s|WSIMPORT|WS%s%s%s|||\n", fund, acct, gl_
acct, desc, transamt, b_month, b_day, b_fullyear, b_month, b_day, b_year)
} else {print $0 >"exception.out" }
}' > ws.out

This include a else clause with a print to an exception file...

HTH

-- Rod Hills
There be dragons...
Volker Borowski
Honored Contributor

Re: AWK Revisited Help

Hello,

my first Idea was like Rodneys approach, but I doubt that you ca use a sepeate redirect inside "awk" (but not sure).

What sure will work is to print a line
EXCEPTION: $0

and divide the resultfile with two greps

grep EXCEPTION ws.dat > except.dat
grep -v EXCEPTION ws.dat > ok.dat

Volker
Darren Etheridge_2
Super Advisor

Re: AWK Revisited Help

Rodney, I tried your suggestion but I keep getting a parse error. Attached is my full script file... any other suggestions on getting the exceptions sent to a seperate file? Thanks
harry d brown jr
Honored Contributor

Re: AWK Revisited Help


Try some perl:

#!/usr/bin/perl
#
use POSIX qw(strftime);
$savedate = strftime "%Y%m%d.%H%M%S", gmtime();

$LOGFL="ws.log";
system("cat ws.datx >> wsdata.dat");

open(LOGFILEptr,">>${LOGFL}");
open(WSerrorfile,">ws.errs");
open(WSnewoutputfile,">ws.out");
open(WSinputfile,"<./wsdata.dat");

printf(LOGFILEptr "Parsing accttest.data @ %s.\n",$savedate);

$x = 0;
$a = "";
while () {
if ( $x == 0 ) {
$b_month = substr($_,0,2);
$b_day = substr($_,2,2);
$b_fullyear = substr($_,4,4);
$b_year = substr($_,6,2);
$x = 1;
} else {
$value1 = substr($_, 0, 14);
$gl_acct = 1234;
# $acct = substr($_, 3, 8);
$damt = substr($_, 92, 13);
$camt = substr($_, 104, 13);
$desc = "W&S BATCH-" . $b_month . $b_day . $b_year;
$transamt = $damt - $camt;
############# WS Case number & Clerk Equiv ##############

$a{"4111151000000"} = "411,2071130";
$a{"11310410110912"} = "411,1041010";
$a{"11310410110913"} = "411,1041010";
$a{"11310410110915"} = "411,1041010";
$a{"43034341010212"} = "411,2074300";
$a{"43034341010311"} = "411,2074300";

############################################################
if ($a{$value1}) {
@b = split(/,/,$a{$value1});
$fund=@b[0];
$acct=@b[1];
printf(WSnewoutputfile "||%s|%s||||%s||%s|%s|N|%s/%s/%s|WSIMPORT|WS%s%s%s|||\n", $fund, $acct, $gl_acct, $desc, $transamt, $b_month, $b_day, $b_fullyear, $b
_month, $b_day, $b_year);
} else {
printf(WSerrorfile $_);
}
}
}
$savedate = strftime "%Y%m%d.%H%M%S", gmtime();
printf(LOGFILEptr "Created ws.out file successfully @ %s.\n",$savedate);


perl and awk syntax are similar, but perl uses 0 as the first element in an array or substring (so I decremented one from the starting positions for substr)

live free or die
harry d brown jr
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: AWK Revisited Help

As I proposed in an earlier thread, but do it right, good perl: better readable, and better maintainable.

my %a = (
4111151000000 => [ 411, 2071130 ],
11310410110912 => [ 411, 1041010 ],
11310410110913 => [ 411, 1041010 ],
11310410110915 => [ 411, 1041010 ],
43034341010212 => [ 411, 2074300 ],
43034341010311 => [ 411, 2074300 ],
);

############################################################
if (exists $a{$value1}) {
my ($fund, $acct) = @{$a{$value1}};

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: AWK Revisited Help

Merijn,

correct as usual. I was just doing a quick and dirty (slap my hand).

live free or die
harry d brown jr
Live Free or Die
Rodney Hills
Honored Contributor

Re: AWK Revisited Help

I guess I missed a }, try-

} else {print $0 >"exception.out" }}

Better would be to use perl, like Procura's solution.

HTH

-- Rod Hills
There be dragons...
Darren Etheridge_2
Super Advisor

Re: AWK Revisited Help

Rodney,
That did the trick!!!! Thank you for all of your help.. I am learning PERL but just not proficent enough to code in it yet :) Thank you all for your help... All are assigned points :)
Darren Etheridge_2
Super Advisor

Re: AWK Revisited Help

Thank you everyone !!!