Unix Shell - Copy files of a certain period (date indicated in the filenames)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > Copy files of a certain period (date indicated in the filenames)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Copy files of a certain period (date indicated in the filenames)
Eric Haijun Xu

2005-11-25, 2:50 am

There is 8 digits in the filename to indicate the date.
*.20051023.*

I have to make up a regex to copy files from 20050922 to 20051021

How? Thanks in advance.

Eric Haijun Xu

2005-11-25, 2:50 am


Eric Haijun Xu wrote:
> There is 8 digits in the filename to indicate the date.
> *.20051023.*
>
> I have to make up a regex to copy files from 20050922 to 20051021
>
> How? Thanks in advance.


The following doesn't seem to work
2005[ 092[2-9] | 093[01] | 10[01][0-9] | 102[01] ]

Eric Haijun Xu

2005-11-25, 2:50 am


Eric Haijun Xu wrote:
> Eric Haijun Xu wrote:
>
> The following doesn't seem to work
> 2005[ 092[2-9] | 093[01] | 10[01][0-9] | 102[01] ]


The following also doesn't seem to work:
*2005{092[2-9],093[01],10[01][0-9],102[01]}*

xicheng

2005-11-25, 2:50 am

Eric Haijun Xu wrote:
> The following doesn't seem to work
> 2005[ 092[2-9] | 093[01] | 10[01][0-9] | 102[01] ]

1) you can not use [ ] to group four characters, use \( and \) instead

2005\(092[2-9]|093[01]|10[01][0-9]|102[01]\)

2) you can not have spaces in your regex.

3) if you can use Perl, you can do sth like this.
foreach(@ARGV) {
if (/2005(\d{4})/) {
if ($1 gt "0922" and $1 lt "1021") {
#do sth here on $_
}
}
}
-----
Good luck
XC

Eric Haijun Xu

2005-11-25, 2:50 am


xicheng wrote:
> Eric Haijun Xu wrote:
> 1) you can not use [ ] to group four characters, use \( and \) instead
>
> 2005\(092[2-9]|093[01]|10[01][0-9]|102[01]\)
>
> 2) you can not have spaces in your regex.
>

$ ls *2005\(092[2-9]|093[01]|10[01][0-9]|102[01]\)*
ksh: 093[01]: not found
ksh: 102[01])*: not found
ksh: 10[01][0-9]: not found
*2005(092[2-9]: No such file or directory

at least not work in Ksh

xicheng

2005-11-25, 2:50 am

Hi, Eric:
I knew nothing about Ksh, but the engine behind the regex is similar.
So we use parenthesis ( ) for grouping, square bracets [] for character
classes, and curly bracets {} for quantifiers. when using these
bracets, you may need to escape some of them in a specific shell, like
\(, \{ \[. check the zsh manual for the use of those metacharacters.

BTW. does "ls" support regex in zsh??? that would be powerful...hehe

XC

Eric Haijun Xu wrote:
> xicheng wrote:
> $ ls *2005\(092[2-9]|093[01]|10[01][0-9]|102[01]\)*
> ksh: 093[01]: not found
> ksh: 102[01])*: not found
> ksh: 10[01][0-9]: not found
> *2005(092[2-9]: No such file or directory
>
> at least not work in Ksh


Eric Haijun Xu

2005-11-25, 2:50 am

Eric Haijun Xu wrote:
> xicheng wrote:
> $ ls *2005\(092[2-9]|093[01]|10[01][0-9]|102[01]\)*
> ksh: 093[01]: not found
> ksh: 102[01])*: not found
> ksh: 10[01][0-9]: not found
> *2005(092[2-9]: No such file or directory
>
> at least not work in Ksh


*2005{092[2-9],093[01],10[01][0-9],102[01]}* works in c shell

I find out a way that works in ksh.
*2005?(092[2-9],093[01],10[01][0-9],102[01])*

Thanks anyway. xicheng

Chris F.A. Johnson

2005-11-25, 2:50 am

On 2005-11-25, Eric Haijun Xu wrote:
> There is 8 digits in the filename to indicate the date.
> *.20051023.*
>
> I have to make up a regex to copy files from 20050922 to 20051021


Why do you need a regex? Extract the date and:

if [ "$date" -gt 20050922 ] && [ "$date" -le 20051021 ]
then
: .......

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Stephane CHAZELAS

2005-11-25, 2:50 am

2005-11-24, 20:57(-08), Eric Haijun Xu:
> There is 8 digits in the filename to indicate the date.
> *.20051023.*
>
> I have to make up a regex to copy files from 20050922 to 20051021
>
> How? Thanks in advance.


If you've got the zsh shell:

setopt extendedglob
cp *.<20050922-20051021>.* /some/dir

Note that this wildcard pattern (shell globbing doesn't involve
regexps) matches integers between 20050922 and 20051021 and
020050925 is one. If that's a problem, you can further restrict
the pattern to be for instance *.????????.*, like in:

cp *.<20050922-20051021>.*~^*.????????.* /some/dir

--
Stéphane
Thorsten Kampe

2005-11-25, 7:49 am

* xicheng (2005-11-25 07:00 +0100)
> I knew nothing about Ksh, but the engine behind the regex is similar.
> So we use parenthesis ( ) for grouping, square bracets [] for character
> classes, and curly bracets {} for quantifiers. when using these
> bracets, you may need to escape some of them in a specific shell, like
> \(, \{ \[. check the zsh manual for the use of those metacharacters.
>
> BTW. does "ls" support regex in zsh??? that would be powerful...hehe


You (and Eric) are confused:
ls is ls - and doesn't care about shells. Shells don't support Regular
Expressions (except bash 3). The pattern matching mechanism is called
"Globbing" and the syntax is quite different.
Stephane CHAZELAS

2005-11-25, 7:49 am

2005-11-25, 09:04(+00), Thorsten Kampe:
> * xicheng (2005-11-25 07:00 +0100)
>
> You (and Eric) are confused:
> ls is ls - and doesn't care about shells. Shells don't support Regular
> Expressions (except bash 3). The pattern matching mechanism is called
> "Globbing" and the syntax is quite different.



Except bash3, ksh93 and zsh (zsh supports perl-like regexps via
a module). Recent ksh93 have extended regexps that it can
convert back and forth to its extended globbing patterns (with
its printf). bash, ksh93 and zsh extended patterns have regexp
operators, though they don't have the same syntax as usual
regexps.

You're right about ls, though.

To answer the question: does zsh support regex in globbing? It
supports regexp with a different syntax, more suitable to match
filenames.

ERE ZSH-extended-patterns
.. ?
* #
+ ##
{} not available unfortunately
^ implicit
$ implicit
[] []
..* *
() ()
....? (|...)
NA ~ (and not)
NA ^ (not)
NA <n-p> (integer range matching)
NA ...

Moreoever, you can apply additional filters to the globbing:

zmodload zsh/pcre

ls -d -- *(e:'[[ $REPLY -pcre-match pcre-regexp ]]'

Or:

m() {
[[ $1 -pcre-match pcre-regexp ]]
}

ls -d -- *(+m)

zsh also supports recursive globbing, **/... and ***/...
(following symlinks).

Latest versions of ksh93 have **/ as well but no filters.

--
Stéphane
Eric Haijun Xu

2005-11-25, 7:49 am

Thorsten Kampe wrote:
> * xicheng (2005-11-25 07:00 +0100)
>
> You (and Eric) are confused:
> ls is ls - and doesn't care about shells.

Anything you type at shell can not avoid shell's attempt to expand the
pattern(s) unless you escape (or quote) it.

> Shells don't support Regular Expressions (except bash 3).

Correct.

>The pattern matching mechanism is called "Globbing" and the syntax is quite >different.


The way to match patterns varies in between different shells and tools.
They are very similar, but not necessary to be consistent at all, they
varies on each program's basis. You can implement a tool use a
completely different pattern matching approach.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com