Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The classic way is the <code>basename</code> command:</p> <pre><code>file="text.rtf" new=$(basename "$file" .rtf).mobi </code></pre> <p>The more modern way avoids exercising other programs:</p> <pre><code>file="text.rtf" new="${file%.rtf}.mobi" </code></pre> <p>If you really must use <code>awk</code>, then I suppose you use:</p> <pre><code>file="text.rtf" new=$(echo "$file" | awk '/\.rtf$/ { sub(/\.rtf$/, ".mobi"); } { print }') </code></pre> <p>For <code>sed</code>, you use:</p> <pre><code>file="text.rtf" new=$(echo "$file" | sed 's/\.rtf$/.mobi/') </code></pre> <p>For a really good explanation of regular expressions, then you want Friedl's <a href="http://regex.info/" rel="nofollow">"Mastering Regular Expressions"</a> book.</p> <hr> <p>To convert <code>text.rtf.mobi</code> to <code>text.mobi</code>, you can use any of the tools previously shown with minor adaptations:</p> <pre><code>new=$(basename "$file" .rtf.mobi).mobi new="${file%.rtf.mobi}.mobi" new=$(echo "$file" | awk '/\.rtf\.mobi$/ { sub(/\.rtf\.mobi$/, ".mobi"); } { print }') new=$(echo "$file" | sed 's/\.rtf\.mobi$/.mobi/') </code></pre> <p>And things are only marginally different if the <code>.rtf</code> can be any other extension, but you start to ask yourself "why doesn't he remove the original extension from the file before converting it, or use the file naming facilities in the converter to get the required output name?"</p> <p>There is no longer a sensible way to do it with <code>basename</code>.</p> <pre><code>new="${file/.[!.]*.mobi/}" # bash new=$(echo "$file" | awk '/\.[^.]+\.mobi$/ { sub(\.[^.]*\.mobi$/, ".mobi"); } { print }') new=$(echo "$file" | sed 's/\.[^.]*\.mobi$/.mobi/') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload