Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
06-30-2009, 06:20 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Rep:
|
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.
|
|
|
|
06-30-2009, 08:51 PM
|
#2
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
How about something like this:
Code:
#!/usr/bin/gawk -f
BEGIN {
FS=OFS=";"
"date +%Y/%m/%d" | getline now
}
ARGIND==1 {
if ($6<now && $4==1)
$4=0
enabled[$1]=$4
print $0
}
ARGIND==2 && FNR==1 {FS=OFS=" "}
ARGIND==2 { print (enabled[$2]?"":"#")$0 }
Last edited by osor; 06-30-2009 at 11:05 PM.
Reason: typo
|
|
|
|
06-30-2009, 09:32 PM
|
#3
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by osor
How about something like this:
Code:
#!/usr/bin/awk -f
BEGIN {
FS=OFS=";"
"date +%Y/%m/%d" | getline now
}
ARGIND==1 {
if ($6<now && $4==1)
$4=0
enabled[$1]=$4
print $0
}
ARGIND==2 && FNR=1 {FS=OFS=" "}
ARGIND==2 { print (enabled[$2]?"":"#")$0 }
|
hi osor,
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.
|
|
|
|
06-30-2009, 09:37 PM
|
#4
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
#!/bin/bash
awk 'BEGIN{
OFS=FS=";"
now=systime()
tempfile = "/var/etc/temp"
tempaccessfile = "/var/etc/tempaccess"
}
FNR==NR{
access[++e]=$0
afile=FILENAME
next
}
$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]
}
}
}
}
END{
cmd = "mv "tempfile" "FILENAME
# write changes to userDB.conf
#system(cmd) #uncomment to use
for(o=1;o<=e;o++){
print access[o] > tempaccessfile
}
cmd = "mv "tempaccessfile" "afile
# write changes to accesslist.conf
# system(cmd) #uncomment to use
}
' accesslist.conf userDB.conf
|
|
|
|
06-30-2009, 09:39 PM
|
#5
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by osor
How about something like this:
Code:
#!/usr/bin/awk -f
BEGIN {
FS=OFS=";"
"date +%Y/%m/%d" | getline now
}
ARGIND==1 {
if ($6<now && $4==1)
$4=0
enabled[$1]=$4
print $0
}
ARGIND==2 && FNR=1 {FS=OFS=" "}
ARGIND==2 { print (enabled[$2]?"":"#")$0 }
|
Hi osor,
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.
|
|
|
|
06-30-2009, 09:53 PM
|
#6
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
Code:
#!/bin/bash
awk 'BEGIN{
OFS=FS=";"
now=systime()
tempfile = "/var/etc/temp"
tempaccessfile = "/var/etc/tempaccess"
}
FNR==NR{
access[++e]=$0
afile=FILENAME
next
}
$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]
}
}
}
}
END{
cmd = "mv "tempfile" "FILENAME
# write changes to userDB.conf
#system(cmd) #uncomment to use
for(o=1;o<=e;o++){
print access[o] > tempaccessfile
}
cmd = "mv "tempaccessfile" "afile
# write changes to accesslist.conf
# system(cmd) #uncomment to use
}
' accesslist.conf userDB.conf
|
Hi ghostdog74,
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.
|
|
|
|
06-30-2009, 09:59 PM
|
#7
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
mktime for GNU awk (gawk).
|
|
|
|
06-30-2009, 10:28 PM
|
#8
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
mktime for GNU awk (gawk).
|
now your script is working good, but i can't find the rest of my users in the usersDB.conf file, it seem that your script save only the disabled users to the temp file.is it possible to add the already enabled users to the temp file.
thanks
|
|
|
|
06-30-2009, 10:32 PM
|
#9
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by magische_vogel
now your script is working good, but i can't find the rest of my users in the usersDB.conf file, it seem that your script save only the disabled users to the temp file.is it possible to add the already enabled users to the temp file.
thanks
|
move the "print $0 > tempfile" after the if (t<now) block.
|
|
|
|
06-30-2009, 10:47 PM
|
#10
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
move the "print $0 > tempfile" after the if (t<now) block.
|
when i move the "print $0 > tempfile" after the if (t<now) block i get all the disbaled users duplicated in the usersDB.conf file, but already enabled users are ok. can you test that please.
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
}
Last edited by magische_vogel; 06-30-2009 at 10:50 PM.
Reason: add a script snipet
|
|
|
|
06-30-2009, 10:59 PM
|
#11
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by magische_vogel
when i move the "print $0 > tempfile" after the if (t<now) block i get all the disbaled users duplicated in the usersDB.conf file, but already enabled users are ok. can you test that please.
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
}
|
I'am sorry, i forget to remove the "print $0 > tempfile" from the if ( t < now ) block.
|
|
|
|
06-30-2009, 11:01 PM
|
#12
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
Originally Posted by magische_vogel
i don't get any results even the script was executed without errors.
|
Probably because I forgot to specify gawk (which has the ARGIND variable). There was also a typo I fixed.
If you want to use nawk, the following should work (I put in some comments):
Code:
#!/usr/bin/awk -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 }
You were correct in how to run it:
Code:
./scritpt.awk usersDB.conf access.conf
Last edited by osor; 07-01-2009 at 12:21 PM.
|
|
|
|
06-30-2009, 11:05 PM
|
#13
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by magische_vogel
I'am sorry, i forget to remove the "print $0 > tempfile" from the if ( t < now ) block.
|
I get a strange behaviour when i run the script twice. All the disabled users are deleted from the usersDB.conf, can you check how your script process disabled users ($4==0).
thanx
|
|
|
|
06-30-2009, 11:24 PM
|
#14
|
|
LQ Newbie
Registered: Jun 2009
Posts: 25
Original Poster
Rep:
|
Quote:
Originally Posted by osor
Probably because I forgot to specify gawk (which has the ARGIND variable). There was also a typo I fixed.
If you want to use nawk, the following should work (I put in some comments):
Code:
#!/usr/bin/awk
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 }
You were correct in how to run it:
Code:
./scritpt.awk usersDB.conf access.conf
|
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
|
|
|
|
06-30-2009, 11:53 PM
|
#15
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by magische_vogel
I get a strange behaviour when i run the script twice. All the disabled users are deleted from the usersDB.conf, can you check how your script process disabled users ($4==0).
thanx
|
by right, you are the one supposed to check, as i can't anticipate what you do at your end. Remove the > tempfile and just print to the screen and see the results. Insert print statements where applicable to see values of variables at different points in your code. That's one of the way to troubleshoot your program. If you are not familiar yet with (g)awk, learn it. See my sig for link to learning gawk
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:16 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|