LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-14-2011, 05:04 PM   #1
Kearneyman
LQ Newbie
 
Registered: Sep 2004
Location: Austin, TX
Distribution: Alpine Linux, Bunsen Labs
Posts: 15

Rep: Reputation: 0
Bash regex to remove files with 3 character followed by a space


What would be the regular expression to allow bash commands (like rm or ls) to find files like: Sep 13, 17.00.00, Feb 14, 12.34.56, Mar 15, 09:12:16.

I have a program which logs to the /var/log/ directory. I want to remove the files from this directory. Currently, I'm using the command
Code:
rm *,*
but I'd like to know if there is a regex I can write which will search for 3 alphabetic characters, followed by a space followed by 2 digits and a comma.

Something like this
Code:
[A-Z][a-z]2[:space:][0-9]2,
The only problem is, that code doesn't work when used in conjunction with ls or rm

I'm sure this is simple, but I'm just curious as to how to accomplish this.

BTW, if this question is answered somewhere else, please just point me to that post.
 
Old 09-14-2011, 06:29 PM   #2
pcardout
Member
 
Registered: Jun 2003
Location: Socorro, New Mexico
Distribution: Debian ("jessie", "squeeze"), Linux Mint (Serena), XUbuntu
Posts: 221

Rep: Reputation: 24
Better than *,*

I don't have a complete answer to your question, but I have something more specific than what you are using now, thus
safer for your rm command.

The code below makes two nonsense files with spaces in the file names after the first two characters, then
two more characters, then a comma and then more characters. It then ls's and finds the files.

(At this
point I refrain from making comments (beyond this one!) about what a pain it is to accommodate the mac and other ``friendly'' OS's
that stick spaces in file names when an underscore is so much less trouble!)

Code:
touch ab\ df,dsfds
touch xr\ df,dfew
richard@jagger:~/temp$ ls -l ??\ ??,*
-rw-r--r-- 1 richard richard 0 Sep 14 17:22 ab df,dsfds
-rw-r--r-- 1 richard richard 0 Sep 14 17:21 xr df,dfew
I do not know how to do the A-Z and 0-9 trick you were suggesting. I will read an answer like that with some interest.
 
Old 09-15-2011, 01:53 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

you could alternatively use bash's extended pattern matching.
Code:
$ shopt -s extglob
$ shopt extglob
extglob        	on
$ ls ?([A-Z][a-z][a-z])\ ?([0-9][0-9]),*
Feb 14, 12.34.56  Mar 15, 09:12:16  Sep 13, 17.00.00
Another possible solution would be - if you insist on using true RegEx - to check each file if it matches a RegEx and then process it:
Code:
for FILE in /path/to/dir/*;do
 if [[ "${FILE##*/}" =~ ^[A-Z][a-z]{2}\ [0-9]{2}, ]];then
  ls "${FILE##*/}"
 fi
done

Last edited by crts; 09-15-2011 at 01:58 AM. Reason: improved RegEx
 
1 members found this post helpful.
Old 09-15-2011, 07:29 AM   #4
Kearneyman
LQ Newbie
 
Registered: Sep 2004
Location: Austin, TX
Distribution: Alpine Linux, Bunsen Labs
Posts: 15

Original Poster
Rep: Reputation: 0
@pcardout
thank you!!!

The [A-Z] and [a-z] just means, well, A-Z. Normally I'd use it like: rm [A-Z]*.[0-9].bz2 to delete the backed up, bzipped log files starting with a capital letter.

The [:space:] is from PHP's POSIX regex library. According to some bash basics that I've read, [:space:] is supported, but I never could get it working.

I did not know about '?'. I've further refined your answer, to one that I feel best suits my needs:


Code:
ls ???\ [0-9][0-9],*
So, you solved my problem! Thanks a million, mate!

Also, this one isn't a friendly OS issue, it's more an issue with many of the new date libraries making the creation of human readable times very easy. People then just pass these strings off to the create file method or logger method and viola! You have a file with spaces. In some ways, I like it because looking at a timestamp and telling the date still ain't easy for me. But then you run into the issue like I just encountered. So, it's a catch 22.

Anyways, You helped me solve my problem! Thank you! I hope this helps other people too.
 
Old 09-15-2011, 07:55 AM   #5
Kearneyman
LQ Newbie
 
Registered: Sep 2004
Location: Austin, TX
Distribution: Alpine Linux, Bunsen Labs
Posts: 15

Original Poster
Rep: Reputation: 0
Thumbs up

crts

Wow. Thank you for taking the time to show me how to do this via a bash script!

I have one question, if you don't mind. Why is it, that I can't use something like
Code:
[0-9]{2}
on the command line, but I can use it in a bash script? That's been my problem all along, I'll try to write a script by first trying my hand at just using the command line, and when a particular line doesn't work, I assume that that line I wrote wasn't valid code and move onto the next thing.

I'll admit, both of your solutions baffle me. I never knew about
Code:
shopt
I'm glad you shared that secret.

For the second script, you mentioned, "true RegEx". I'm assuming that means POSIX regex, but if there's a difference, could you tell me (you don't have to answer me right away, but I'm very curious).


And BTW, all three of the solutions you both provided worked on Centos5.6 as well as Mac OS X y'all are amazing.
 
Old 09-15-2011, 08:31 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Kearneyman View Post
I have one question, if you don't mind. Why is it, that I can't use something like
Code:
[0-9]{2}
on the command line, but I can use it in a bash script?
Well, bash can only interpret RegEx via its '=~' operator. This operator can only be used inside the '[[' command.
Otherwise bash can only interpret globs aka wildcards. By activating
shopt -s extglob

It can also interpret extended globs aka extended pattern matching.

Quote:
I'm glad you shared that secret.
It's not really a secret
Try 'man bash' and search for 'shopt' to get an overview of supported options.
Quote:
For the second script, you mentioned, "true RegEx".
I simply meant RegEx. I wrote "true" RegEx to emphasize that the previous solution does not use a RegEx although there is a certain resemblance between RegEx and globs. Sorry for the confusion.

I hope this clears things up.
 
1 members found this post helpful.
Old 09-15-2011, 02:28 PM   #7
Kearneyman
LQ Newbie
 
Registered: Sep 2004
Location: Austin, TX
Distribution: Alpine Linux, Bunsen Labs
Posts: 15

Original Poster
Rep: Reputation: 0
@crts

Quote:
I simply meant RegEx. I wrote "true" RegEx to emphasize that the previous solution does not use a RegEx although there is a certain resemblance between RegEx and globs. Sorry for the confusion.

I hope this clears things up.
Yes, you cleared things up. Thanks again for all the help. And for taking the time to explain things to me.

You both have certainly helped me very much.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I remove everything after a certain character in text files? SentralOrigin Linux - General 3 01-14-2009 02:15 PM
bash script to remove everything in a line before a space tallmtt Programming 2 12-28-2008 07:40 PM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
Remove rightmost character from bash gn00kie Programming 9 01-29-2008 12:08 AM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 03:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration