lweegp
April 16, 2008, 4:40am
1
hi all scripting gurus,
need some guide and advise from you.
i'm trying to list all the files in the year 2004 and the file format is something like this: 11176MZ00004JV900004JVB00004JVCcDBU20041206.txt try to use the symbol ^ but somehow it does not help.
i try this as well: ls -ltr | grep 200401 | more and it does shows the files listed in 2004 but also in 2005 and 2006. any way i can refine this search? :o
can anyone help? many thanks.
wee
era
April 16, 2008, 4:47am
2
If there are files from 2005 and 2006 which coincidentally contain 200401 in their file name then you need to make the regular expression more exact. If the date is always just before a final .txt extension then grep 2004....\.txt$ might work better. (Just a dot means "any character" in a regular expression.)
lweegp
April 16, 2008, 5:15am
3
hi era,
thanks for the guide. tried the command but getting this result:
spids111% ls -ltr |grep *2004....\.txt$* | more
No match
spids111%
somewhere gone wrong in my command line?
thanks again!
wee
lweegp
April 16, 2008, 5:18am
4
lweegp:
hi era,
thanks for the guide. tried the command but getting this result:
spids111% ls -ltr |grep *2004....\.txt$* | more
No match
spids111%
somewhere gone wrong in my command line?
thanks again!
wee
it works!! sorry i put in the *...oops...many thanks!!
wee
lweegp
April 16, 2008, 5:22am
5
apologies...one more question.
if i want to remove the files in 2004 what is the next command i need to put in? using rm command? like this:
spids111% ls -ltr | grep 2004....\.txt$ | rm * ?
era
April 16, 2008, 5:27am
6
rm doesn't read standard input, it expects the files to remove on its command line. Use xargs or backticks.
rm `ls -ltr | grep 2004....\.txt$`
or
ls -ltr | grep 2004....\.txt$ | xargs rm
Try it with "echo" instead of "rm" to make sure that you're doing the right thing.
era
April 16, 2008, 5:27am
7
(Weird, double post, sorry.)
lweegp
April 16, 2008, 6:18am
8
era,
thanks a million it works!
wee
lweegp
April 16, 2008, 6:26am
9
ooops....it does not work...got the following:
spids111% ls -ltr | grep 2004....\.txt$ | xargs rm
rm: illegal option -- w
rm: illegal option -- -
rm: illegal option -- w
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
usage: rm [-fiRr] file ...
somewhere went wrong? apologies...:o
wee
era
April 16, 2008, 6:54am
10
Yes, take out the options from ls, you just want the file names -- it's trying to remove the permissions, the owners, and the date stamps, too. Oops.
lweegp
April 16, 2008, 11:45pm
11
era:
Yes, take out the options from ls, you just want the file names -- it's trying to remove the permissions, the owners, and the date stamps, too. Oops.
hi era,
sorry for the trouble...:o
i try the following:
spids111% grep 2004....\.txt$ | xargs echo
but nothing returns...some part went wrong? thanks.
wee
era
April 17, 2008, 2:31am
12
You took out too much. You still need ls to list the files, only not in "long format".
ls | grep 2004....\.txt$ | xargs echo rm
I put in an "echo" so you see what it's doing. Take out the echo if the result looks correct.
lweegp
April 17, 2008, 4:18am
13
lweegp:
hi era,
sorry for the trouble...:o
i try the following:
spids111% grep 2004....\.txt$ | xargs echo
but nothing returns...some part went wrong? thanks.
wee
hi era...wonderful!! it works perfectly fine now! thanks so much!!