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!

Awk Syntax 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have the following awk statement which I am trying to modify so that if $5 does not contain an A-Z (middle initial), field 5 will be null rather than containing the next field, the date.

awk 'BEGIN{printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", "Emp #", "Last", "First", "MI", "Start Date", "Dept", "ETR", "TS")}
{if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31"
printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6,$7, $8)}' newhires.list > newhires.rpt

I tried adding something like: " ; if ($5 !~ "[A-Z]$" $5 = ""))" to the if statement, but that does not work properly. Any help would be greatly appreciated.

Thanks,

John
 
Regexp syntax is not valid within quotes.
if ($5 !~ /[0-9]{5}/) {$5 = ""}
 
[tt]
if ($5 ~ !/^[A-Z]$/) {
$8 = $7
$7 = $6
$6 = $5
$5 = ""
}
[/tt]

Jean Pierre.
 
Sorry,
[tt]
if ($5 !~ /^[A-Z]$/) {
$8 = $7
$7 = $6
$6 = $5
$5 = ""
}
[/tt]


Jean Pierre.
 
Jean Pierre,

Thank you. I basically understand what I'm looking for, but I still cannot seem to incorporate it with my existing if statement. The line below is doing the filtering, but I also need to have the if statement regarding field 5 as well.

{if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31")


Thanks,

John
 
I thought it would work with the following:

BEGIN{printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", "Emp #", "Last", "First", "MI", "Start Date", "Dept", "ETR", "TS")}

/ETR/{if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31" && $5 ~ "[A-Z]")
printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6,$7, $8)}

else if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31" && $5 !~ "[A-Z]")
{ $8 = $7
$7 = $6
$6 = $5
$5 = ""

printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6,$7, $8)}

but I am still getting an error. Any help would be greatly appreciated.

Thanks,

John
 
Does this do what you want?

/ETR/{
if ($1 ~ /[0-9]{5}/ && $2 !~ /[a-z]{3}/ && $0 !~ "ETR31") {
if ($5 !~ /[A-Z]/) {
$8 = $7
$7 = $6
$6 = $5
$5 = ""
}
printf (....
}
}

CaKiwi
 
CaKiwi,

Thank you for your help, but unfortunately, that does not work either. Perhaps I should have posted the sample input sooner.

EMP ID LAST FIRST M DATE DEPT ETR TS
89788 SCHMOE JOE O 07-FEB-04 H1250 ETR11 No
89811 jjg0 GEE JAY J 07-FEB-04 B1855 ETR13 No
89796 WATSON ADAM M 07-FEB-04 H1627 ETR31 No
46782 sxh3 HOLT SAM X 03-JAN-04 A1209 ETR22 No

What I want to display is all the lines that:

1. Contain a 5 digit string in the first field
2. Do not have a user ID in the second field
3. Do not contain the string ETR31

And if field 5 is null, I want to print it as a null field rather than printing it as the contents of field 6.

The desired output from the example above is:

EMP ID LAST FIRST M DATE DEPT ETR TS
89788 SCHMOE JOE O 07-FEB-04 H1250 ETR11 No

Thanks,

John
 
Try this
BEGIN {...}
NR>1 && !/ETR31/ {
if ($1 ~ /[0-9]{5}/ && substr($0,7,3) !~ /[a-z]{3}/) print
}

CaKiwi
 
Thanks again CaKiwi, but it still does not take into consideration the fact that field 5 may be null.

Thanks,

John
 
Your solution works fine if I was using print, but for formatting purposes I would like to use printf.
 
Post an example of an input record that does not print correctly.

CaKiwi
 
Maybe

NR>1 && !/ETR31/ {
if ($1 ~ /[0-9]{5}/ && substr($0,7,3) !~ /[a-z]{3}/) {
if (substr($0,28,1) == " ") {
$8=$7
$7=$6
$6=$5
$5=""
}
printf ...
}
}

CaKiwi
 
Or even

NR>1 && !/ETR31/ {
if ($1 ~ /[0-9]{5}/ && substr($0,7,3) !~ /[a-z]{3}/) {
if (substr($0,28,1) == " ") {
$8=$7
$7=$6
$6=$5
$5=$4
$4=""
}
printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6,$7, $8)
}
}

CaKiwi
 
Thank you for your help, CaKiwi!! I finally figured out what I was doing wrong. I was calling field 4 field 5 but since there was no data in the real field 5, it's field 4. Below is the final awk script.

BEGIN{printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", "Emp #", "Last", "First", "MI", "Start Date", "Dept", "ETR", "TS")}

/ETR/{if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31" && $4 !~ "[0-9]") printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6, $7, $8)

else if ($1 ~ "[0-9]{5}" && $2 !~ "[a-z]{3}" && $0 !~ "ETR31" && $4 !~ "^[A-Z]$")
{ $8 = $7
$7 = $6
$6 = $5
$5 = $4
$4 = ""

printf ("%-7s %-15s %-15s %-3s %-10s %-6s %-6s %-4s\n", $1, $2, $3, $4, $5, $6,$7, $8)
}
}

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top