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 <tt> 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.


# An application used to replace all instances of <tt>, <code>, and <console> in a wiki page
# with their corresponding updated tags.



replaceCharacters() {
    while read line; do
            if [[ $line =~ \<code\> ]]; then
                    let "CODE_COUNT++"

                    if [[ $line =~ \<\/code\> ]]; then
                            let "END_COUNT++"
                            echo ${replace_string} >> ${NEW_FILE_LOCATION}
                            echo "[Line ${LINE_COUNT}]: Found <code> and </code>."
                            echo ${replace_string} >> ${NEW_FILE_LOCATION}
                            echo "[Line ${LINE_COUNT}]: Found <code>"
            elif [[ $line =~ \<tt\> ]]; then
                    let "TT_COUNT++"

                    if [[ $line =~ \<\/tt\> ]]; then
                            let "END_COUNT++"
                            echo ${replace_string} >> ${NEW_FILE_LOCATION}
                            echo "[Line ${LINE_COUNT}]: Found <tt> and </tt>"
                            echo ${replace_string} >> ${NEW_FILE_LOCATION}
                            echo "Line ${LINE_COUNT}]: Found <tt>"
            elif [[ $line =~ \<console\> ]]; then
                    let "CONSOLE_COUNT++"
                    echo ${replace_string} >> ${NEW_FILE_LOCATION}
                    echo "[Line ${LINE_COUNT}]: Found <console>"
            elif [[ $line =~ \<\/[ct][ot].*\> ]]; then
                    echo ${replace_string} >> ${NEW_FILE_LOCATION}
                    echo "[Line ${LINE_COUNT}]: Found an end (</tt>,</console> or </code>)"
                    let "END_COUNT++"
                    echo ${line} >> ${NEW_FILE_LOCATION}
            let "LINE_COUNT++"
    done < "$FILE_TO_CONVERT"

displayReplaceInfo() {
    echo -e "\n================="
            echo ${count}: $((count))

# Run the functions

In particular, the replaceCharacters() function seems quite redundant. How can I simplify this code?

Here is an example of what a page I might be parsing through looks like:

=== Installing the Stage 3 tarball ===

Now that filesystems are created and your hardware and system clock are     set, the next step is downloading the initial Stage 3 tarball. The Stage 3 is a pre-compiled system used as a starting point to install Funtoo Linux. Notice: if you're using virtual machines (like Vbox) generic stage3 images are preferred rather than cpu-optimized ones.

To download the correct build of Funtoo Linux for your system, head over to the [[Subarches]] page. Subarches are builds of Funtoo Linux that are designed to run on a particular type of CPU, to offer the best possible performance. They also take advantage of the instruction sets available for each CPU.

The [[Subarches]] page lists all CPU-optimized versions of Funtoo Linux. Find the one that is appropriate for the type of CPU that your system has, and then click on its name in the first column (such as <code>corei7</code>, for example.) You will then go to a page dedicated to that subarch, and the available stage3's available for download will be listed.

For most subarches, you will have several stage3's available to choose from. This next section will help you understand which one to pick.

==== Which Build? ====

'''If you're not sure, pick <code>funtoo-current</code>.'''

Funtoo Linux has various different 'builds':

<tr><th class="info">Build</th><th class="info">Description</th></tr>
<tr><td><code>funtoo-current</code></td><td>The most commonly-selected build of Funtoo Linux. Receives rapid updates and preferred by desktop users.</td></tr>
<tr><td><code>funtoo-stable</code></td><td>Emphasizes less-frequent package updates and trusted, reliable versions of packages over the latest versions.</td></tr>

==== Which Variant? ====

'''If you're not sure, pick <code>standard</code>.'''

Our "regular" stage3's are listed with a variant of <code>standard</code>. The following variant builds are available:

<tr><th class="info">Variant</th><th class="info">Description</th></tr>
<tr><td><code>standard</code></td><td>The "standard" version of Funtoo Linux</td></tr>
<tr><td><code>pure64</code></td><td>A 64-bit build that drops multilib (32-bit compatibility) support. Can be ideal for server systems.</td></tr>
<tr><td><code>hardened</code></td><td>Includes PIE/SSP toolchain for enhanced security. PIE does require the use of PaX in the kernel, while SSP works with any kernel, and provides enhanced security in user-space to avoid stack-based exploits. For expert users.</td></tr>

==== Download the Stage3 ====

Once you have found the stage3 that you would like to download, use <code>wget</code> to download the Stage 3 tarball you have chosen to use as the basis for your new Funtoo Linux system. It should be saved to the <code>/mnt/funtoo</code> directory as follows:

<console># ##i##cd /mnt/funtoo
# ##i##wget http://build.funtoo.org/funtoo-current/x86-64bit/generic_64/stage3-latest.tar.xz

2 Answers 2


The way you replace characters is extremely tedious, hard to follow, and error prone. A lot easier way is using sed. Here's an example to get you started, replacing two patterns:

sed -e 's/<tt>/{{c|/g' -e 's?</tt>?}}?g' input.txt > output.txt

This won't print such details like "Line ${LINE_COUNT}]: Found <tt>" 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.

  • \$\begingroup\$ sed had seemed like a good initial approach to me. For some reason, however, I got the idea that it would be unable to handle some of the characters contained by the wiki pages. Apparently that was false. \$\endgroup\$ Commented Jul 1, 2015 at 20:08
  • \$\begingroup\$ @DuncanG.Britton do you mean that the sed command I gave works? \$\endgroup\$
    – janos
    Commented Oct 11, 2015 at 5:43

Your logic looks like it would break if a line had <tt>x=1</tt> <code>, it would hit the code block first and skip the tts.

You don't need as much branching structure, you could go for:

if [ $replaced!=$line ]; then
    echo "[Line $LINE_COUNT]: found <code>"

if [ $replaced!=$line ]; then
    echo "[Line $LINE_COUNT]: found <tt>"
echo ${replace_string} >> ${NEW_FILE_LOCATION}

It would end up slightly shorter, clearer and able to handle mixed things on one line.

With Python, you could pair up the patterns and replacements, and have a clear loop:

pairs = [('<tt>',       '{{c|'),
         ('</tt>',      '}}'),
         ('<code>',     '{{c|'),
         ('</code>',    '}}'),
         ('<console>',  '{{console|body='),
         ('</console>', '}}')]

counts = {}
# ..open wiki_file and output file..

for line_num, line in enumerate(wiki_file, 1):
    for old_text, new_text in pairs:
        if old_text in line:
            counts[old_text] = counts.get(old_text, 0) + 1
            line = line.replace(old_text, new_text)
            print "[Line {}]: found {}".format(line_num, old_text)

    # print line to new file

print counts

This isn't to say "you should use Python"*, it's to offer a different structure that's probably possible in any scripty/interpreted language (but I can't write Bash like that off the top of my head).

*you should, though

  • \$\begingroup\$ Is the variable pairs a dictionary? \$\endgroup\$ Commented Jul 1, 2015 at 20:46
  • \$\begingroup\$ No, it's a list of tuples (fixed lengh lists). The code isn't tested, btw. \$\endgroup\$ Commented Jul 1, 2015 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.