LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 06-30-2009, 06:20 PM   #1
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Rep: Reputation: 0
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.
 
Old 06-30-2009, 08:51 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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
 
Old 06-30-2009, 09:32 PM   #3
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
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.
 
Old 06-30-2009, 09:37 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
 
Old 06-30-2009, 09:39 PM   #5
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
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.
 
Old 06-30-2009, 09:53 PM   #6
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 06-30-2009, 09:59 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
mktime for GNU awk (gawk).
 
Old 06-30-2009, 10:28 PM   #8
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
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
 
Old 06-30-2009, 10:32 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by magische_vogel View Post
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.
 
Old 06-30-2009, 10:47 PM   #10
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
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
 
Old 06-30-2009, 10:59 PM   #11
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by magische_vogel View Post
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.
 
Old 06-30-2009, 11:01 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by magische_vogel View Post
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.
 
Old 06-30-2009, 11:05 PM   #13
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by magische_vogel View Post
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
 
Old 06-30-2009, 11:24 PM   #14
magische_vogel
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
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
 
Old 06-30-2009, 11:53 PM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by magische_vogel View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good time to use AWK? VeeDubbs Linux - General 9 03-13-2009 10:55 PM
bash script read error and awk ouptut error whited Programming 4 10-16-2007 07:05 PM
Amarok 1.4.5 and some .asf audio filea raylhm Suse/Novell 3 03-25-2007 09:37 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
This is read time !!! neo77777 General 3 11-20-2002 05:22 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration