Create rewrite rules from new and old URLs using the power of Vim to munge text
As part of the move of Agaric content from Agaric.com to data.agaric.com, we needed to create a bunch (1,592 to be precise) redirects that look like this one:
RewriteRule ^nice-menus-drop-down-bug http://data.agaric.com/node/1048 [NC,R=301,L]
Stefan, in his wizardry, had already pulled a tab-separated-value file of the old path to the new URL, so that what we had was:
nice-menus-drop-down-bug http://data.agaric.com/node/1048
And it needs to look like the above RewriteRule line.
Pulling up the file in a vi editor, consulting a 500 page book on Vim, and then getting help from Stefan, resulted in a command like the below (the colon is just the usual start-of-command symbol).
:%s/^(.)\t(.)$/RewriteRule ^\1 \2 [NC,R=301,L]
In this:
- the %s specifies that this should be searched and replaced globally
- the first slash / kicks off the search portion of the command
- the carrot ^ denotes the beginning of a line
- the \( and \) are grouping characters (parenthesis) that had to be escaped with the backslash
- each .* matches any number (the asterisk) of any character (the dot), until the next match
- the \t matches a tab
- the second slash / starts the replacement pattern portion of the command
- RewriteRule ^ is literally that text, as part of the replacement
- \1 is the result of the first grouped pattern (created by the parenthesis)
- \2 is the result of the second grouped pattern, that is, whatever was matched by any .* (any number of any characters) between the tab and the end of the line, is inserted here
- [NC,R=301,L] is more plain text, ending the replacement
So ah, vim, simple. Sort of. But replacing 1592 lines at once was cool.
Comments
Post new comment