- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK Script Problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:11 AM
тАО05-27-2005 03:11 AM
cat /pei/peifas/ws/wsdata.dat |
awk 'BEGIN { FS=" "; }
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
{
if (value1 = "4111151000000")
{ fund=411
acct=2071130
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)
}
}'
Darren
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:24 AM
тАО05-27-2005 03:24 AM
Solutionif (value1 == "411115100000")
Rather then using a case statement for multiple tests, you can use the the array syntax-
a["411115100000"]="411,2071130"
a["411222200000"]="511,3082240"
...
split(a[value1],b,",")
fund=b[1]
acct=b[2]
...
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:29 AM
тАО05-27-2005 03:29 AM
Re: AWK Script Problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:31 AM
тАО05-27-2005 03:31 AM
Re: AWK Script Problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:32 AM
тАО05-27-2005 03:32 AM
Re: AWK Script Problem
Simplified output from a2p
--8<---
#!/usr/bin/perl
$[ = 1; # set array base to 1
$FS = ' ';
while (<>) {
chomp; # strip record separator
print $_ if $value1 = substr($_, 1, 14);
print $_ if $gl_acct = 1234;
#acct = substr($0, 4, 8)
print $_ if $damt = substr($_, 93, 13);
print $_ if $camt = substr($_, 105, 13);
print $_ if $desc = 'W&S BATCH-' . $b_month . '' . $b_day . '' . $b_year;
print $_ if $transamt = $damt - $camt;
if ($value1 = '4111151000000') {
$fund = 411;
$acct = 2071130;
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;
}
}
-->8---
I know perl much better than awk, and looking at that code, it's probably not what you want
I also don't see b_month, b_day, b_year, and b_fullyear being defined anywhere
In the if statement, you are using = instead of ==, causing an asignment instead of a comparison
--8<---
#!/usr/bin/perl
while (<>) {
chomp;
my ($b_day, $b_month, b_year) = (localtime)[3,4,5];
my $b_fullyear = $b_year + 1900;
my $gl_acct = 1234;
my ($value1, $damt, $camt) = unpack "A14 x79 A13 A13", $_;
my $transamt = $damt - $camt;
my $desc = sprintf "W&S BATCH-%4d%02d%02d", $b_fullyear, $b_month + 1, $b_day;
$value1 eq "4111151000000" or next;
my $fund = 411;
my $acct = 2071130;
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;
}
-->8---
Do my comments make any sense?
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:34 AM
тАО05-27-2005 03:34 AM
Re: AWK Script Problem
awk 'if (substr($0,0,4) == "abcd") { print $0 }'
By redirecting STDOUT to a file, then the lines will be printed to a file...
If you want to get more complicated, like several output files, then you can add "> file" to the print statement.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:43 AM
тАО05-27-2005 03:43 AM
Re: AWK Script Problem
--8<--- 246551.pl
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
my ($b_day, $b_month, $b_year) = (localtime)[3,4,5];
my $b_fullyear = $b_year + 1900;
my $gl_acct = 1234;
my ($value1, $damt, $camt) = unpack "A14 x79 A13 A11", $_;
#print;
#print "$value1|$damt|$camt|\n";
my $transamt = $damt - $camt;
my $desc = sprintf "W&S BATCH-%4d%02d%02d", $b_fullyear, $b_month + 1, $b_day;
$value1 eq "4111151000000" or next;
my $fund = 411;
my $acct = 2071130;
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;
}
-->8---
lt09:/tmp 117 > perl 246551.pl 246551.dat
||411|2071130||||1234||W&S BATCH-20050527|-16.48|N|4/27/2005|WSIMPORT|WS427105|||
lt09:/tmp 118 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:44 AM
тАО05-27-2005 03:44 AM
Re: AWK Script Problem
for any statement which is not covered by brackets, awk prints the entire record. so to fix your problem
start '{' after BEGIN block.
eg:
awk 'BEGIN { FS=" "; "
{
value1 = substr
...
}'
also the if condition value being used is one character short in length in the dat file you have given. check that too.
as far as i know there is no switch case functionality in awk.
Regards,
Gopi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:56 AM
тАО05-27-2005 03:56 AM
Re: AWK Script Problem
If you are only interested in "4111151000000" at the beginning of your record you should code a grep and pipe into awk :
grep ^"4111151000000" /pei/peifas/ws/wsdata.dat | awk '{
...
}'
also the test will be
if(value1=="...")
Regards
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 04:31 AM
тАО05-27-2005 04:31 AM
Re: AWK Script Problem
I respectfully disagree with Jean-Luc's suggestion to filter through grep first.
AWK is good at, and happy to, look for the match itself? Why shove the data through grep, into a pipe, into awk?
The same applies to Darren's suggested use of 'cat' to read the file and feed it to awk.
Awk is perfectly happy to read the file itself! Furthermore, awk (or perl) will have the actual filename available which may be useful for more elaborte scripts with error handlers.
And a field seperator of " " is default.
So the basic awk syntax for this problem is simply:
awk '/^4111151000000/ { fund=... print...}' pei/peifas/ws/wsdata.dat
fwiw,
Hein.