Last active
October 5, 2015 10:28
-
-
Save Asenar/2794113 to your computer and use it in GitHub Desktop.
massive search and replace multiline when you have an old html website to modify. Theses scripts has been used to add google analytic tracker in <head> , and to convert an html website to "basic php", replacing html main blocs like headers, footers and menus by a php inclusion + renaming file html to php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Michaël Marinetti | |
# Description: in each files ( $ext extension) | |
# if they not contains $search add $content (absolute path) before $add_before (a vim regex, beginning with / or ?) | |
dir=www | |
ext="htm" | |
search="_setAccount" | |
content=~/correct-html-header | |
add_before="/<\/head>/-1" | |
find $dir -name "*.$ext" -type f \! -exec grep -q "$search" {} \; -print | \ | |
while read file; do | |
echo "starring $file " | |
vim -e -s "$file" +$add_before +':r!cat $content' +wq | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" This script assume the "common blocs" have already been created, and all bloc content (<head>, ...) are already handled by theses blocs | |
" Call me that way in a shell : | |
" for i in *.php; do print "processing $i ..."; | |
" vim -S command_list.vim $i; echo "done"; | |
" done; | |
" | |
" 1) save title | |
/<title> | |
:s#<title>\(.*\)</title>#\$title = '\1';# | |
:yank t | |
" 2) remove head content | |
" find <head>, mark to < | |
/<head>/+1 | |
:mark a | |
" find </head>, mark to > | |
/<\/head>/-1 | |
:mark b | |
:'a,'bd | |
:i | |
<?php | |
. | |
" go to the end of the line to insert title | |
:normal! <End> | |
" insert title | |
:normal! "tp | |
:a | |
require('blocs/commun/head.php'); | |
?> | |
. | |
/<object | |
:mark a | |
:/<\/object> | |
:mark b | |
:'a,'bd | |
:i | |
<?php require('blocs/commun/entete.php'); ?> | |
. | |
/<\/body>/-1 | |
:i | |
<?php require('blocs/commun/pied.php'); ?> | |
. | |
:1 | |
:wq | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment