use awk to read and update FileA and FileB at the same time
Hi everybody,
Here is my awk script : controle_accounts.awk contents: #! /usr/bin/awk -f BEGIN { FS=";"; # Changing the field separator "date +%Y/%m/%d" | getline now; }{ #if account is expired and account not disabled then disable account if ($6<now && $4==1) {$4=0; //disable the account in the usersDB.conf only. How to disable the corresponding account in the accesslist.conf file should go here also.} print ($1 ";" $2 ";" $3 ";" $4 ";" $5 ";" $6 ";" $7) > "/var/etc/usersDB.conf"; //write changes to input file. } usersDB.conf description : username;name;role;account_status;registration_date;expiration_date;note usersDB.conf contents: user1;geroge;U;1;2009/03/01;2009/06/01;none user2;piere;U;1;2009/04/15;2009/07/15;none user3;albert;U;1;2009/06/01;2009/07/30;none user4;philipe;U;1;2009/06/01;2009/06/25;none user5;michael;U;1;2009/06/01;2009/09/30;none user6;jack;U;1;2009/06/01;2009/08/30;none user7;ronald;U;1;2009/06/01;2009/05/30;none user8;patrick;U;1;2009/06/01;2009/04/19;none accesslist.conf description : username;password accesslist.conf contents: U: user1 kenav7wr U: user2 cxq978fk U: user3 rkl749w2 U: user4 krvv56of U: user5 879yrw55 U: user6 49452ayd U: user7 37adck6r U: user8 cxyo1gah note : the primary key between the usersDB.conf and the accesslist.conf is the username My question is how to disable the expired account in the second file named accesslist.conf by adding # in front of U: (same question for enabling the account) using the above awk programme. expected results : usersDB.conf contents: user1;geroge;U;0;2009/03/01;2009/06/01;none user2;piere;U;1;2009/04/15;2009/07/15;none user3;albert;U;1;2009/06/01;2009/07/30;none user4;philipe;U;0;2009/06/01;2009/06/25;none user5;michael;U;1;2009/06/01;2009/09/30;none user6;jack;U;1;2009/06/01;2009/08/30;none user7;ronald;U;0;2009/06/01;2009/05/30;none user8;patrick;U;0;2009/06/01;2009/04/19;none accesslist.conf contents: #U: user1 kenav7wr U: user2 cxq978fk U: user3 rkl749w2 #U: user4 krvv56of U: user5 879yrw55 U: user6 49452ayd #U: user7 37adck6r #U: user8 cxyo1gah /var/etc/controle_accounts.awk usersDB.conf Any help will be appreciated. |
How about something like this:
Code:
#!/usr/bin/gawk -f |
Quote:
i make copy paste your script into then i excute it that way : ./controle_accounts.awk usersDB.conf accesslist.conf i don't get any results even the script was executed without errors. can you give more details for each statement in your script. where the changes are saved ? in each input file or to the standard output ? thanx. |
Code:
#!/bin/bash |
Quote:
When i run your script (./controle_accounts.awk usersDB.conf accesslist.conf) after i copy and past it to controle_accounts.awk file i don't get any results and any errors. Can you give more details on each statement you use in your script. did your script update the usersDB.conf and accesslist.conf files to reflect the last changes before it exit. best regards. |
Quote:
I get this error when i run your script: awk: line 39: function mktime never defined Can you explain how your script process the files. Thanks. |
mktime for GNU awk (gawk).
|
Quote:
thanks |
Quote:
|
Quote:
thanx $4==1{ m=split($6,d,"/") #split date on / strd = d[1]" "d[2]" "d[3]" 0 0 0" t=mktime(strd) #get seconds if ( t < now ){ $4=0 print $0 > tempfile for(o=1;o<=e;o++){ if ( access[o] ~ $1){ access[o] = "#"access[o] } } } print $0 > tempfile } |
Quote:
|
Quote:
If you want to use nawk, the following should work (I put in some comments): Code:
#!/usr/bin/awk -f Code:
./scritpt.awk usersDB.conf access.conf |
Quote:
thanx |
Quote:
when i run the script i get the folowing errors: debiansrv:/var/etc# ./test2 usersDB.conf accesslist.conf awk: ./test2 awk: ^ syntax error awk: ./test2 awk: ^ regular expression not terminated can you check what happens please. thanks |
Quote:
|
Quote:
|
Quote:
#!/usr/bin/gawk -f FNR==1 { # when we encounter line 1 of a file FS=OFS= arg++ ? " " : ";" # arg = 1 for first file, 2 for second "date +%Y/%m/%d" | getline now } arg==1 { if ($6<now && $4==1) $4 = 0 enabled[$1] = $4 # record the enabled status of this username print $0 > "usersDB.conf" } arg==2 { print (enabled[$2] ? "" : "#") $0 > "access.conf" } thanks a lot. |
Quote:
Code:
#!/usr/bin/awk -f |
Quote:
String comparison is done as you would expect, and most charsets (including ASCII) are created so that digits follow the normal rules for succession (including 0 < 1). As long as all fields are padded (which in this case they are), the string comparison should suffice for dates. |
All times are GMT -5. The time now is 08:22 PM. |