Fli
09-09-2016, 11:37 AM
Before doing search and replace, backup data.
search and replace in file:
sed -i "s|search|replace|g" file
search and replace in the variable
$variable="$(echo "$variable"|sed "s|search|replace|g")
search and replace only if line contains defined phrase:
sed -i "/Next/ s|#||g" file
(replace # by nothing (remove) on the line that contains Next)
search and replace multiple phrases in file:
sed -i "s|search|replace|g;s|search2|replace2|g"
Preview changes instead of writing them into file:
cat "filename"|sed "s|search|replace|g"
search and replace in all directory files containing username .php:
for file in $(find -A1 /path/to/filesdir/|grep -v "./"|grep ".php");do sed -i "s|search|replace|g" $file;done
NOTES:
backup before doing search&replace
one can use / or other character instead of |
some characters like slash and quotation mark needs to be escaped by backslash, example: sed -i "s|<img src=\"\" \/>|<img src=\"url\" \/>|g" file
search and replace in file:
sed -i "s|search|replace|g" file
search and replace in the variable
$variable="$(echo "$variable"|sed "s|search|replace|g")
search and replace only if line contains defined phrase:
sed -i "/Next/ s|#||g" file
(replace # by nothing (remove) on the line that contains Next)
search and replace multiple phrases in file:
sed -i "s|search|replace|g;s|search2|replace2|g"
Preview changes instead of writing them into file:
cat "filename"|sed "s|search|replace|g"
search and replace in all directory files containing username .php:
for file in $(find -A1 /path/to/filesdir/|grep -v "./"|grep ".php");do sed -i "s|search|replace|g" $file;done
NOTES:
backup before doing search&replace
one can use / or other character instead of |
some characters like slash and quotation mark needs to be escaped by backslash, example: sed -i "s|<img src=\"\" \/>|<img src=\"url\" \/>|g" file