Find the answer to your Linux question:
Results 1 to 4 of 4
Hi there I have file called users and there expierd dates in this format user1 12/5/2006 user2 13/6/2006 user3 24/4/2006 .... i need help to creat a simple script to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    20

    Red face shell script and date ... any help


    Hi there
    I have file called users and there expierd dates in this format
    user1 12/5/2006
    user2 13/6/2006
    user3 24/4/2006
    ....

    i need help to creat a simple script to print any user < or = the current date
    to the file called ( expusers )


    any idea ??

    Regards
    AD

  2. #2
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    I have not more ideas than a painful parsing way... That format is not very standard, so you are in troubles. I recommend that you try to change to an UNIX format, i.e. time in seconds.

    However, try to play with date command.
    date +%D outputs similar to your format (swaping month and date)

    Best regards

  3. #3
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    Do the dates have to be in that format??

    If so, then you could use some combination of awk, date and a check to do something like this.


    If it does not need to be in that format, you could try using the Unix timestamp type format and then it's simply a case of checking for anything greater than the date's unix timestamp.

    Awk comes into this in that it will allow you to return the date with something like:
    Code:
    awk '{print $2}' users
    Will return:
    12/5/2006
    13/6/2006
    24/4/2006
    so you can then compare that date to the current date, and then modify the lines as required.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  4. #4
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    So then you'd need to switch from day/month/year (for all entries in the file) to unix timestamp.
    Code:
    $ date -dtoday '+%d/%m/%Y %s'
    03/05/2006 1146658238
    Just playing around with this idea:
    Code:
    $ cat > file
    user1 12/5/2006
    user2 13/6/2006
    user3 24/4/2006
    user4 12/3/2004
    $ sed 's#\([^ ]*\)\ \([^/]*\)/\([^/]*\)/\([^/]*\)#\1\4\3\2#g' file
    user12006512
    user22006613
    user32006424
    user42004312
    $ sed "s#\([^ ]*\)\ \([^/]*\)/\([^/]*\)/\([^/]*\)#\1=\"\`date --date='\4/\3/\2\' +%s\`\"#g" file
    user1="`date --date='2006/5/12' +%s`"
    user2="`date --date='2006/6/13' +%s`"
    user3="`date --date='2006/4/24' +%s`"
    user4="`date --date='2004/3/12' +%s`"
    If you'd put those four lines into a script and echo the contents (in seconds since 1970):
    Code:
    $ echo $user4
    1079046000
    You can then compare the contents of these variables with the timestamp.
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •