LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   use awk to read and update FileA and FileB at the same time (https://www.linuxquestions.org/questions/linux-newbie-8/use-awk-to-read-and-update-filea-and-fileb-at-the-same-time-736801/)

ghostdog74 06-30-2009 11:56 PM

Quote:

Originally Posted by osor (Post 3592418)
comments):
Code:

..
        if ($6<now && $4==1)
        ...


i really doubt 2 dates can be compared when in this format YYYY/MM/DD.

magische_vogel 07-01-2009 06:05 AM

Quote:

Originally Posted by osor (Post 3592418)
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

Your script is working fine except when i run it twice it deactivate already deactivated users i mean it add # to "#F: username" in the access.conf file. is it possible to ignore the already deactivated users in the access.conf file.

#!/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.

osor 07-01-2009 12:21 PM

Quote:

Originally Posted by magische_vogel (Post 3592436)
when i run the script i get the folowing errors:

Another typo (seems to be a theme with me in this thread)! I of course meant
Code:

#!/usr/bin/awk -f

osor 07-01-2009 12:21 PM

Quote:

Originally Posted by ghostdog74 (Post 3592469)
i really doubt 2 dates can be compared when in this format YYYY/MM/DD.

Sure they can (at least in ASCII and its supersets including UTF-8 and all 15 parts of ISO-8859).

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 09:20 AM.