Last active
August 2, 2018 09:45
-
-
Save teamshortcut/11f5e834102c69d444451831379e924e to your computer and use it in GitHub Desktop.
Bash script to go through multiple files looking for certain lines using regex (differing between files) and replacing it with the correct corresponding text (different for each file), grabbed from a list in an external file
This file contains hidden or 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 | |
#Bash script to go through multiple files looking for certain lines using regex (differing between files) | |
#and replacing it with the correct corresponding text (different for each file), grabbed from a list in an external file | |
#Expects to be placed in the same directory as the files to search through | |
#Relies on 2 external files; a list of filenames to edit, and the corresponding replacement text for each file (separated by newlines) | |
#Used to escape any problematic characters in the replacement text | |
escapeCharacter() #Takes 2 arguments, the character to escape and the text to escape it from | |
{ | |
escapedCharacter="\\"$1 | |
echo ${2//$1/$escapedCharacter} | |
} | |
#The filenames of the lists of files and replacement text | |
replacementListFilename="replacementlist" | |
fileListFilename="filelist" | |
#Creates 2 arrays | |
replacementList=() | |
fileList=() | |
#Assigns all lines from bundlelist to the replacementList array | |
#Also optionally escapes any characters that aren't regex-friendly, such as a colon; this can be removed and the character to escape can be edited | |
output=$(cat $replacementListFilename) #relies on a bundlelist file containing all 25 bundles, separated by newlines | |
characterToEscape=":" | |
count=0 | |
for i in $output | |
do | |
#replacementList[$count]=$i | |
replacementList[$count]=$(escapeCharacter $characterToEscape $i) | |
((count++)) | |
done | |
#Assigns all lines from filelist to the fileList array | |
output=$(cat $fileListFilename) #relies on a filelist file containing all 25 filenames, separated by newlines | |
count=0 | |
for i in $output | |
do | |
fileList[$count]=$i | |
((count++)) | |
done | |
regexStart="s/" #Opening of the regex statement | |
regexSearch="render[(]['].*\//render(\'" #The regex for the text to be searched for; replace with your own regex expression | |
regexEnd="/g" #This is the required ending of the regex, used by sed when replacing text | |
#For each of the 25 files... (the number can be edited) | |
for ((i=0; i <= 24; i++)) | |
do | |
echo ${fileList[$i]} #...outputs the filename... | |
#...and uses sed to replace the text that is searched for, by the the corresponding replacement text for each file | |
sed -i -e $regexStart$regexSearch${replacementList[$i]}$regexEnd ${fileList[$i]} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment