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 USING AWK TO EXTRACT DATA FROM MULTIPLE LINES OF A FILE

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I am trying to extract some data from a file which contains information about users' accounts. There are approximately 20 lines of information per user, however I am only interested in a few fields from each line. Below is a sample of some of the lines in the file.

Username: USER1 Owner: Joe Schmoe
Account: SYSTEMS UIC: [16,226] ([PRTMGR,USER])
CLI: DCL Tables: DCLTABLES
Default: USER:[USER1]
LGICMD:
Flags: DisUser
Primary days: Mon Tue Wed Thu Fri
Secondary days: Sat Sun
Primary 000000000011111111112222 Secondary 000000000011111111112222
Day Hours 012345678901234567890123 Day Hours 012345678901234567890123
Expiration: (none) Pwdminimum: 6 Login Fails: 0
Pwdlifetime: 30 00:00 Pwdchange: 30-JUL-2002 15:11
Last Login: 15-AUG-2002 14:58 (interactive), 23-NOV-2001 11:24 (non-interactive)

I would like to extract the following fields for all users who have more than 1 login failure: Username, Owner, Login Fails, and Last Login.

Any help would be greatly appreciated.

Thanks,

John
 
Here's something to get you started. It is left as an exercise for the reader to improve the print format.

BEGIN{FS=":"}
/^Username/ { usr = substr($2,1,13); own = $3; logfails = 0}
/^Expiration/ {logfails = $4}
/^Last Login/ {if (logfails) {print usr " " own " " logfails; print}} CaKiwi
 
Here is an uglier version that should do the job if
you incorporate the end section into main(), just as an example for your paragraph data example.

IF(probably won't) you have trouble with cakiwi's field selection the match() function should do the right thing.


function derive_base(val1) {
gsub(/[^0-9]/,"",val1)
return val1
}


{
if (match($0,/^Username:.[a-zA-Z0-9]+/)) {
Uvar = substr($0,RSTART,RLENGTH)
Ovar = substr($0,RLENGTH + 1,length($0) - RLENGTH + 1)
}

if ($0 ~ /Expiration/) {
match($0,/Login Fails:.*/)
Loginvar = substr($0,RSTART,RLENGTH)
}
match($0,/Last Login:.*$/)
Latlogvar = substr($0,RSTART,RLENGTH)

}
END {
if ( (derive_base(Loginvar)) > 0) {
print Uvar
print Ovar
print Loginvar
print Latlogvar
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top