Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to modify paths with a certain pattern.

For example

./foo/1023023/43655345/bar/

becomes

./foo/_dir_/_dir_/bar/

The pattern is always preceded by /foo/ followed by 2 directories that have numbers as a name (the strings to replace with the tag dir).

I tried using sed, but my regex skills aren't up to par with this problem. I don't get how to replace both directory strings with the tag.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Your problem specification is incomplete; is the tag fixed, or does it depend on the numbers or some other aspect of the path? Is it the same in both replacements?

The simple solution (simplest assumptions for answers to the above) is

sed 's,/foo/[0-9][0-9]*/[0-9][0-9]*/,/foo/_dir_/_dir_/,'

but I would imagine the actual problem doesn't involve fixed _dir_ strings.

share|improve this answer
    
This is actually exactly what I wanted. Do you know a good resource to learn regex? I looked at several tutorials online, but they all seem very incomplete. I'm new to cygwin in general. –  Millianz Apr 6 '12 at 18:27
    
The canonical resource is Jeffrey Friedl's Mastering Regular Expressions; any online tutorial will be incomplete, simply because there is no single "regex" — it's a family of similar but not entirely compatible languages, and most tutorials focus on some particular regex implementation (usually Java or PHP in my experience, modern versions of which use PCRE). sed in particular uses POSIX basic regular expressions, which are much simpler (easier to learn, but often require more repetition). So it's hard to point at a good resource smaller than a fairly thick book. –  geekosaur Apr 6 '12 at 18:41
    
Hmm ok, I will look at some books in that case. How would I expand the above example when the numbers are in hex, i.e. 0-9,A-F are valid digits. –  Millianz Apr 6 '12 at 19:05
    
That would be [0-9A-F] instead of simply [0-9]. You may also want the -i option if the digits might be either upper or lower case. –  geekosaur Apr 6 '12 at 19:10
    
Ok I see. I tried with a comma which didn't work. So if I wanted to ignore case I'd either do [0-9a-fA-F] or start sed with the -i option? –  Millianz Apr 6 '12 at 19:17
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.