patternbashMinor
Bash script to replace substrings in a file with strings
Viewed 0 times
scriptsubstringsfilewithreplacestringsbash
Problem
I am an editor of the Funtoo Linux wiki and was looking to create a shell script that would help to ease the updating of pages on the wiki that use old tags by replacing certain deprecated tags with their newer counterparts. For example, I am interested in replacing every occurrence of `
#!/bin/bash
# An application used to replace all instances of , , and in a wiki page
# with their corresponding updated tags.
FILE_TO_CONVERT="toConvert"
NEW_FILE_LOCATION="converted"
LINE_COUNT=1
CODE_COUNT=0
TT_COUNT=0
CONSOLE_COUNT=0
END_COUNT=0
replaceCharacters() {
while read line; do
if [[ $line =~ \ ]]; then
let "CODE_COUNT++"
if [[ $line =~ \ ]]; then
let "END_COUNT++"
replace_string=${line///"{{c|"}
replace_string=${replace_string///"}}"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found and ."
else
replace_string=${line///"{{c|"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found "
fi
elif [[ $line =~ \ ]]; then
let "TT_COUNT++"
if [[ $line =~ \ ]]; then
let "END_COUNT++"
replace_string=${line///"{{c|"}
replace_string=${replace_string///"}}"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found and "
else
replace_string=${line///"{{c|"}
with {{c|. I wrote a bash script that accomplishes the task required, but seems to be somewhat bloated and 'hackish' and I am looking for ways that I can consolidate what I have written.
``#!/bin/bash
# An application used to replace all instances of , , and in a wiki page
# with their corresponding updated tags.
FILE_TO_CONVERT="toConvert"
NEW_FILE_LOCATION="converted"
LINE_COUNT=1
CODE_COUNT=0
TT_COUNT=0
CONSOLE_COUNT=0
END_COUNT=0
replaceCharacters() {
while read line; do
if [[ $line =~ \ ]]; then
let "CODE_COUNT++"
if [[ $line =~ \ ]]; then
let "END_COUNT++"
replace_string=${line///"{{c|"}
replace_string=${replace_string///"}}"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found and ."
else
replace_string=${line///"{{c|"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found "
fi
elif [[ $line =~ \ ]]; then
let "TT_COUNT++"
if [[ $line =~ \ ]]; then
let "END_COUNT++"
replace_string=${line///"{{c|"}
replace_string=${replace_string///"}}"}
echo ${replace_string} >> ${NEW_FILE_LOCATION}
echo "[Line ${LINE_COUNT}]: Found and "
else
replace_string=${line///"{{c|"}
Solution
The way you replace characters is extremely tedious, hard to follow, and error prone. A lot easier way is using
Here's an example to get you started, replacing two patterns:
This won't print such details like
If on the other hand those diagnostic lines are important to you,
then you can either write another script to generate those details,
or rewrite this script in another language that's better suited for line-by-line processing and transformation, like Python or Perl.
sed.Here's an example to get you started, replacing two patterns:
sed -e 's//{{c|/g' -e 's??}}?g' input.txt > output.txtThis won't print such details like
"Line ${LINE_COUNT}]: Found " as your original, but hopefully that's not too important to you.If on the other hand those diagnostic lines are important to you,
then you can either write another script to generate those details,
or rewrite this script in another language that's better suited for line-by-line processing and transformation, like Python or Perl.
Code Snippets
sed -e 's/<tt>/{{c|/g' -e 's?</tt>?}}?g' input.txt > output.txtContext
StackExchange Code Review Q#95481, answer score: 3
Revisions (0)
No revisions yet.