Results 1 to 6 of 6

Thread: Help me use grep with regex please; i know the JS version but it doesnt seem to work

  1. #1
    Join Date
    Jul 2011
    Beans
    2

    Help me use grep with regex please; i know the JS version but it doesnt seem to work

    Im creating a bash script for a project that will search through my list of backups which are all named in the format: -

    dd.mm.yyyy-hh.mm (the last one is hour hour minute minute)

    in javascript regular expressions seemed to be pretty easy to do and to find a file in february in JS it would be something like ---


    /^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/

    but ive tried all different ways to achieve the same result as the one above, ive searched online their doesnt seem to be any good sites for regex in bash, what would be an equivilent in bash

    basically i want to plug this regular expression into this line of code here : -

    find ~/the/directory -type f -exec basename {} \; | grep '/^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/' > file.list

    if i can get this line of code to work then I will be able to make a loop on it that will turn the files which matched the regular expression into a zipped tarball for archiving purposes.

    P.S. I know this regex doesnt check for date validity its purely intended to find files whose names correspond to feb.2011 for example.

    Anyone who could give me anyhelp converting this regex so it will work in bash will be awesome.

    THANKY YOU

  2. #2
    Join Date
    Feb 2011
    Location
    new york state
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Help me use grep with regex please; i know the JS version but it doesnt seem to w

    Quote Originally Posted by maxymus View Post
    Im creating a bash script for a project that will search through my list of backups which are all named in the format: -

    dd.mm.yyyy-hh.mm (the last one is hour hour minute minute)

    in javascript regular expressions seemed to be pretty easy to do and to find a file in february in JS it would be something like ---


    /^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/

    but ive tried all different ways to achieve the same result as the one above, ive searched online their doesnt seem to be any good sites for regex in bash, what would be an equivilent in bash

    basically i want to plug this regular expression into this line of code here : -

    find ~/the/directory -type f -exec basename {} \; | grep '/^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/' > file.list

    if i can get this line of code to work then I will be able to make a loop on it that will turn the files which matched the regular expression into a zipped tarball for archiving purposes.

    P.S. I know this regex doesnt check for date validity its purely intended to find files whose names correspond to feb.2011 for example.

    Anyone who could give me anyhelp converting this regex so it will work in bash will be awesome.

    THANKY YOU
    maybe a for loop ?
    for f in [0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]-[0-9][0-9].[0-9][0-9]; do tar ... "$f"; done
    Power tends to corrupt, and absolute power corrupts absolutely. Great men are almost always bad men, even when they exercise influence and not authority

  3. #3
    Join Date
    Jan 2012
    Beans
    342

    Re: Help me use grep with regex please; i know the JS version but it doesnt seem to w

    maxymus ...

    There is no need to grep for the string, find can search based on modification times, and as these are backups their respective modification times will reflect the search string ... so:

    Code:
    find /path -type f -newermt 2011-02-01 ! -newermt 2011-02-28 >> file.list
    This will find all files with modification times for the month of Feb 2011.

    HTH ... khay

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,924
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Help me use grep with regex please; i know the JS version but it doesnt seem to w

    Quote Originally Posted by maxymus View Post
    Im creating a bash script for a project that will search through my list of backups which are all named in the format: -

    dd.mm.yyyy-hh.mm (the last one is hour hour minute minute)

    in javascript regular expressions seemed to be pretty easy to do and to find a file in february in JS it would be something like ---


    /^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/

    but ive tried all different ways to achieve the same result as the one above, ive searched online their doesnt seem to be any good sites for regex in bash, what would be an equivilent in bash

    basically i want to plug this regular expression into this line of code here : -

    find ~/the/directory -type f -exec basename {} \; | grep '/^([0-9][0-9)\.02\.2011\-([09][09])\.([09][09])$/' > file.list

    if i can get this line of code to work then I will be able to make a loop on it that will turn the files which matched the regular expression into a zipped tarball for archiving purposes.

    P.S. I know this regex doesnt check for date validity its purely intended to find files whose names correspond to feb.2011 for example.

    Anyone who could give me anyhelp converting this regex so it will work in bash will be awesome.

    THANKY YOU
    Maybe you are getting a bit too stringent here. Something like
    Code:
    find -name "??.02.2011-??.??"
    would be more than enough if your directory structure isn't too polluted ('?' matches a single character). Otherwise, find supports the [] syntax:
    Code:
    find -name "[0-9][0-9].02.2011-[0-9][0-9].[0-9][0-9]"

  5. #5
    Join Date
    Jul 2011
    Beans
    2

    Re: Help me use grep with regex please; i know the JS version but it doesnt seem to w

    Thank you so much guys, I actually found a way in the end, i think it was just a slight syntax difference between JS expressions and bash expressions, I think youve given me some really good ideas to simplify my expression, i did think there would be an easier way to do it, im going to try doing it with just find tomorrow and see how that goes and ill let you all know.

    I think in my regex i just put a / at the end and a / at the beggining like in JS, when this is not required for bash, after that it worked lol

    find /media/Backup/Backups/CIS -type d -exec basename {} \; | grep '^[0-9][0-9].1.2012' > /home/max/file.list

    and this worked pefectly

    then i was able to do a loop on the file that coppied it to another location for acrhiving purposes.

    But thank you so much for answering, its really given me some extra ideas and understanding after struggling so long on linux manuals (lol)
    Last edited by maxymus; February 28th, 2012 at 12:09 AM. Reason: I didn't see the previous posts before I posted

  6. #6
    Join Date
    Jan 2012
    Beans
    342

    Re: Help me use grep with regex please; i know the JS version but it doesnt seem to w

    Quote Originally Posted by maxymus View Post
    Thank you so much guys, I actually found a way in the end [...]
    Your welcome ... but you may, as ofnut's suggests, be overdoing it.

    Quote Originally Posted by maxymus View Post
    Code:
    find /media/Backup/Backups/CIS -type d -exec basename {} \; | grep '^[0-9][0-9].1.2012' > /home/max/file.list
    The question is whether the '|grep' is necessary. Does the following not produce the same output?

    Code:
    find /media/Backup/Backups/CIS -type d -newermt 2012-01-01 ! -newermt 2012-01-31 > /home/max/file.list
    If these are dated backup dirs that were created on the dated suffix then it should produce the same output as the '|grep'.

    Doesn't matter I suppose, but there is always an overhead when piping, and so if the task can be achieved without it .. bonus. Anyhow, thought it worth drawing your attention too.

    best ... khay

Bookmarks

Posting Permissions

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