Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 02-17-2008, 07:53 PM   #1
LQ Newbie
Registered: Feb 2008
Posts: 5

Rep: Reputation: 0
Unhappy Rename files by stripping text from either desired string?


I just can't get my head round this regexp thing.

Basically, I have a folder full of files, and i want to rename them. Each file has a particular string in it (of a particular format) that i want to isolate, but strip text from either side, and keep a bit of the text plus an extension at the end.

Consider this:

i hope you can follow that. i want to strip the start, then keep the x1y1 (but this changes depending on the file!), remove a bit of text after this and then keep the next part plus the extension.

there is probably an easier way of explaining how to do what i need, but i just wanted to be clear (or try to be).

basically, all the files should end up after rename as

Btw, the dots you see in the filename don't necessarily have to exist, except for this bit which is absolute: .keepthispartandthe.ext, and must have the dots where they are.

hope that clears it up for anyone who can help!

Old 02-17-2008, 09:27 PM   #2
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,444
Blog Entries: 4

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Okay, so what you are saying seems to be this:
  1. "The part that you are interested in" consists of: one-or-more 'word characters' (alpha, numeric, or underscore), followed by a literal period, followed by one-or-more word characters, all anchored at the end-of-line.
  2. To put it another way, the "right way" to find "the part that you are interested in" would be to examine the string from right to left. The pattern you're looking for always ends at the end of the string, extending leftwards past the extension and the obligatory period, thence only to the extent of the rightmost group of word-characters thereafter. Anything (to the left) beyond that is uninteresting.
In that case, look at this regular-expression:
  • The pattern describes what you do want, not the cruft which may surround it.
  • The entire regular-expression is, by convention, enclosed in slashes, which are not part of the pattern.
  • The trailing '$' at the end of the pattern denotes the end-of-string: this pattern always consists of the rightmost characters in the string.
  • Parentheses "()" are used to enlose a group, which will allow you to extract that group from the string: once you've found that a string matches the pattern, you can extract what substring matched each group.
  • "\w" is a metacharacter for "word character." It represents a predefined group of characters as previously described, corresponding to "[A-Za-z0-9_]"
  • "\." refers to a literal period.
  • "+" is a repetition factor, in this case "one or more of."
  • So the pattern within the group can be read as, "one or more of word-characters, followed by one period, followed by one or more of word-characters, followed by the end of the string."
Old 02-21-2008, 10:04 PM   #3
LQ Newbie
Registered: Feb 2008
Posts: 5

Original Poster
Rep: Reputation: 0
A very in-depth reply, I must say, and thank you also for explaining each part of the expression. Problem is, I have never had to deal with regular expressions in my lifetime of using Windows; I only recently got a dedicated linux box - so would you mind giving me an example of how i would rename the files using the regexp you suggest?



files, rename

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Convert DOS text files to UNIX text files ta0kira Linux - Software 7 03-15-2011 11:42 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Networking&Text files; A string or two, and LOTS of int's quentusrex Programming 9 10-27-2004 05:19 AM
RPM: Copying files to desired loc using %install directive raqueeb Linux - Software 0 02-12-2004 10:47 PM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:52 PM.

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