Skip to content

Instantly share code, notes, and snippets.

@ml-eds
Last active August 26, 2020 06:48
Show Gist options
  • Save ml-eds/c2433598fdfb9f477252145e5d16e87d to your computer and use it in GitHub Desktop.
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
#!/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