Results 1 to 10 of 13
I am writing a script to select certain fields from a list of user information to generate usernames and passwords
This is my script
Code:
echo "UsrName:Psswd"
while read line
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-15-2013 #1Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
If statement with awk
I am writing a script to select certain fields from a list of user information to generate usernames and passwords
This is my script
This is the output: using example infoCode:echo "UsrName:Psswd" while read line do echo $line | awk '{print tolower($1":")tolower(substr($6,1,1))tolower(substr($7,1,1))tolower(substr($8,1,1))tolower(substr($10,1,1))(substr($12,9,4))} ' done < studentlist
UsrName:Psswd
jcc2380:jbci9445
jac1936:sbci7993
acc4636:jbcc0437
spf1696:jbcc2398
sek8835:fbcc7149
mck0599:jbcc0000
gjm1887:sbci4354
ngm5479:jbci8268
ztp7190:fbci1298
axs9097:bcbt
rjs6624:sbcc2021
mws1990:sbci0581
The problem im running into is that all of the rows has 12 fields except one row has 11 fields which is why the 10th password is incomplete. Is there anyway i can do an if statement saying ..... If there are 12 fields then use the code i have up there, else do the code i would write for 11 fields.... Any help would be appreciated. Thanks
- 02-15-2013 #2Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
You're over complicating things.
Here, the while loop is useless.
awk can read files line by line.
What do you call fields? I see nothing 12, or 11.
There are 12 lines with data, plus 1 header.
- 02-15-2013 #3Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
The while loop was a mistake I forgot to take it out of the script. When I am speaking of the 11 and 12 fields i am speaking in regards of the list of data i have that i need to generate usernames and passwords from, not the output i posted above^.
Here is example list of data I have
JCC2380 XXX-XX-XXXX CAREY, JULIE C JR-II BISS CPSC BS INFO TECH 412/779-9445
JAC1936 XXX-XX-XXXX CONSOLMAGNO, JOE A. SO-I BISS CPSC BS INFO TECH 412/833-7993
ACC4636 XXX-XX-XXXX CURRIE, ADAM C. JR-II BISS CPSC BS COMP SCI 724/657-0437
SPF1696 XXX-XX-XXXX FARLEY, SEAN P. JR-I BISS CPSC BS COMP SCI 814/236-2398
SEK8835 XXX-XX-XXXX KELLEY, SHAWN E. FR-II BISS CPSC BS COMP SCI 724/406-7149
MCK0599 XXX-XX-XXXX KOMISIN, MICHAEL C. JR-I BISS CPSC BS COMP SCI 000/000-0000
GJM1887 XXX-XX-XXXX MACK, GREGORY J. SO-II BISS CPSC BS INFO TECH 724/735-4354
NGM5479 XXX-XX-XXXX MCINTIRE, NICHOLAS G JR-I BISS CPSC BS INFO TECH 724/794-8268
ZTP7190 XXX-XX-XXXX PETAK, ZACHARY T. FR-II BISS CPSC BS INFO TECH 724/406-1298
AXS9097 XXX-XX-XXXX SAUNDERS, ALEXANDER SO-I BISS CPSC BS INFO TECH 724/334-0313
RJS6624 XXX-XX-XXXX SNYDER, ROBERT J. SO-II BISS CPSC BS COMP SCI 814/328-2021
MWS1990 XXX-XX-XXXX STASNY, MATTHEW W SO-II BISS CPSC BS INFO TECH 412/882-0581
If you notice in the 10th row down the name is missing an initial.... this is causing me problems
Here is how I was thinking of approaching it
I just need to know what/how to put it in my if statment....Code:echo "UsrName:Psswd" if [ ] then awk '{print tolower($1":")tolower(substr($6,1,1))tolower(substr($7,1,1))tolower(substr($8,1,1))tolower(substr($10,1,1))(substr($12,9,4))} ' $1 else awk '{print tolower($1":")tolower(substr($5,1,1))tolower(substr($6,1,1))tolower(substr($7,1,1))tolower(substr($9,1,1))(substr($11,9,4))} ' $1 fi
For example: IF THEIR ARE 12 FIELDS EXECUTE THE FIRST COMMAND AND IF THEIR ARE 11 FIELDS EXECUTE THE SECOND COMMAND
Thanks
- 02-15-2013 #4Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
why is this file so badly formated?
Code:awk '{ NF==12?a="6 7 8 10":a="5 6 7 9"; n=split(a,A," "); printf("do stuff with $1; "); for(i=1;i<=n;i++){printf("do stuff with $%d; ", i, n)} print "do stuff with "NF}' UrFile
- 02-16-2013 #5Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
Because the people like to make everything difficult. And im not allowed to modify the file... it would be so much easier if i could modify it but oh welll
- 02-16-2013 #6Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
Im running into trouble with what i should put in here. The help is appreciatedCode:{printf("do stuff with $%d; ", i, n)} print "do stuff with "NF}'
- 02-16-2013 #7Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
actually just this part
Code:{printf("do stuff with $%d; ", i, n)}
- 02-16-2013 #8Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
oops, my bad

delete the ,n part.
I can't correct above.
edit:Code:awk '{ NF==12?a="6 7 8 10":a="5 6 7 9" n=split(a,A," ") printf("do stuff with $1; ") for(i=1;i<=n;i++){printf("do stuff with $%d; ", i)} print "do stuff with "NF }' UrFile
n doesn't bother finally.
replacewithCode:"do stuff with $%d; ",i)
Code:"%s", tolower(substr(i,1,1))
- 02-16-2013 #9Just Joined!
- Join Date
- Apr 2012
- Posts
- 15
Np thank you, appreciate the help
- 02-17-2013 #10Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
grrr
that'sCode:"%s ", tolower(substr($A[i],1,1)))


Reply With Quote
