Last active
August 26, 2020 06:48
-
-
Save ml-eds/c2433598fdfb9f477252145e5d16e87d to your computer and use it in GitHub Desktop.
Shell script to replace strings in files based on two file-based lists
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 | |
# given list_search.txt with content, e.g. | |
# <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0" /> | |
# <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /> | |
# given list_replace.txt with content, e.g. | |
# <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5" /> | |
# <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" /> | |
oifs=$IFS # save a copy of the input field separator character list | |
IFS=$'\n' | |
search=( $(< list_search.txt) ) | |
replace=( $(< list_replace.txt) ) | |
IFS=$oifs # restore | |
for ((i=0; i<${#search[@]}; i++)); do | |
# echo "First parameter : ${search[$i]} -- second parameter : ${replace[$i]}" | |
n=${search[$i]} | |
k=${replace[$i]} | |
# do not use / as a sed delimiter - it will fail when replacing xml | |
find . -type f -name \*.csproj -exec sed -i '' -e "s#$n#$k#g" '{}' + | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment