Replace Certain Elements in a Path with a Tag

0

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.

Millianz

Posted 2012-04-06T18:08:29.377

Reputation: 81

Answers

1

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.

geekosaur

Posted 2012-04-06T18:08:29.377

Reputation: 10 195

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 – 2012-04-06T18:27:26.217

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 – 2012-04-06T18:41:01.150

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 – 2012-04-06T19:05:39.477

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 – 2012-04-06T19:10:16.347

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 – 2012-04-06T19:17:17.527

Yes; either one would work (and both would be pointless). – geekosaur – 2012-04-06T19:19:42.403