Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For posterity, let's post the solution from within VI. You can remove the <kbd>Ctrl</kbd>-<kbd>M</kbd> at the end of every line like this:</p> <pre><code>:%s/^V^M$// </code></pre> <p>Note that this is what you <em>type</em>, wnere <code>^V</code> means <kbd>Ctrl</kbd>-<kbd>V</kbd> and <code>^M</code> means <kbd>Ctrl</kbd>-<kbd>M</kbd>. The idea here is that <code>^V</code> will "escape" the following ^M, so that you can match it in the substitution regex.</p> <p>And the <code>%</code> expression means "do this on every line".</p> <p>Note that this may or may not work in vim, depending on your settings.</p> <p>But your question asks how to do this in vi, in which you can't easily make a change to multiple files. If you're open to using other tools, please indicate so in your question.</p> <p>You can use sed on a single file or stream:</p> <pre><code>$ printf 'one\r\ntwo\r\n' &gt; /tmp/test.txt $ od -c &lt; /tmp/test.txt 0000000 o n e \r \n t w o \r \n 0000012 $ sed -i'' -e 's/^M$//' /tmp/test.txt $ od -c &lt; /tmp/test.txt 0000000 o n e \n t w o \n 0000010 $ </code></pre> <p>In this case, in /bin/sh in FreeBSD, I escaped the <code>^M</code> by ... you guessed it ... using <code>^V</code>.</p> <p>When using sed's <code>-i</code> option, you can specify multiple files and they will all be modified in place, perhaps eliminating the need to wrap this in a script. If you want to put this into a script anyway, I recommend you try to do so, and then ask for help if it doesn't work. That's the StackOverflow Way. :-)</p> <p>Or just use Jonathan's for loop example. You don't need temp files.</p> <p><strong>UPDATE</strong></p> <p>If your <code>sed</code> does not have a <code>-i</code> option, then you can still do this pretty easily in a for loop:</p> <pre><code>[ghoti@pc ~]$ od -c /tmp/test1.txt 0000000 o n e \r \n t w o \r \n 0000012 [ghoti@pc ~]$ for f in /tmp/test*.txt; do sed -e 's/^M$//' "$f" &gt; /tmp/temp.$$ &amp;&amp; mv -v /tmp/temp.$$ "$f"; done /tmp/temp.26687 -&gt; /tmp/test1.txt /tmp/temp.26687 -&gt; /tmp/test2.txt [ghoti@pc ~]$ od -c /tmp/test1.txt 0000000 o n e \n t w o \n 0000010 </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