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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help : Verify a file with awk

Status
Not open for further replies.

samtg

Programmer
Jul 22, 2003
8
MA
Hi
i have a file and i want to do some controls on it, for example :
1. column number 3 must be <> 0
2. lenght of column number 4 must be = 12
3. column number 2 must exist on a table on database informix

file whith a pipe delimiter :

2002|x|125464|000012356984|1000.00|500.00|500.00|
2002|y|0|125489632158|5000.00|1000.00|4000.00|
2002|z|126589|12548|2000.00|500.00|1500.00|
2002|a|256894|000000215698|1000.00|200.00|800.00|
2002|y|125698|000012349658|200.00|10.00|190.00|

table on database contains :
x
y
z

the results will be 2 files :
file 1 contains : (good records)
2002|x|125464|000012356984|1000.00|500.00|500.00|
2002|y|125698|000012349658|200.00|10.00|190.00|

file 2 contains : (bad records)
2002|y|0|125489632158|5000.00|1000.00|4000.00|
2002|z|126589|12548|2000.00|500.00|1500.00|
2002|a|256894|000000215698|1000.00|200.00|800.00|

thanks a lot.
 
I assumed the database codes are in a text file and I printed out only bad records.

awk -v fn=&quot;samtg.tab&quot; -f samtg.awk infile

# ------ samtg.awk ------
BEGIN {
FS = &quot;|&quot;
if (!fn) fn = &quot;samtg.tab&quot;
j = 1
while ((getline t[j] < fn) > 0) j++
nt = j-1
}
{
if ($3 == 0 || length($4) != 12) {
print
next
}
j=1
while (j<=nt && t[j] != $2) j++
if (j>nt) {
print
}
}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Try this:
Code:
awk -F'|' '
BEGIN{while((getline<&quot;/path/to/table.unl&quot;)>0)X[$1]++}
$3!=0 && length($4)==12 && X[$2]>0{print >&quot;/path/to/file1&quot;}
{print >&quot;/path/to/file2&quot;}
' /path/to/file.unl

Hope This Help
PH.
 
Try this:

# cat samtg.awk
BEGIN {
FS=&quot;|&quot;
GOOD=&quot;good&quot;
BAD=&quot;bad&quot;

if (!fn) fn = &quot;samtg.tab&quot;
while((getline < fn) > 0)
arrA[$1] = $0
}

{
if (($2 in arrA) && $3!=0 && length($4)==&quot;12&quot;) {
print $0 >> GOOD}

else
{print $0 >> BAD}
}

# awk -v fn=&quot;samtg.tab&quot; -f samtg.awk infile
 
thank you cawiki for your help.thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top