Text Processing: Remove Carriage Return: Difference between revisions

From WikiMLT
mNo edit summary
m (Text replacement - "mlw-continue" to "code-continue")
 
Line 11: Line 11:
sed -e 's/^M//g' input-file > output-file
sed -e 's/^M//g' input-file > output-file
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" class="mlw-continue">
<syntaxhighlight lang="shell" class="code-continue">
# GNU/sed syntax
# GNU/sed syntax
</syntaxhighlight>
</syntaxhighlight>
Line 17: Line 17:
sed -i 's/^M//g' input
sed -i 's/^M//g' input
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" class="mlw-continue">
<syntaxhighlight lang="shell" class="code-continue">
# Replace it with FOO
# Replace it with FOO
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1" class="mlw-continue mlw-shell-gray">
<syntaxhighlight lang="shell" line="1" class="code-continue mlw-shell-gray">
sed -i -e 's/^M/FOO/g' input
sed -i -e 's/^M/FOO/g' input
</syntaxhighlight>
</syntaxhighlight>
Line 28: Line 28:
sed 's/\r$//g' input-file > output-file
sed 's/\r$//g' input-file > output-file
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" class="mlw-continue">
<syntaxhighlight lang="shell" class="code-continue">
# GNU/sed syntax
# GNU/sed syntax
</syntaxhighlight>
</syntaxhighlight>
Line 47: Line 47:
== A note about deleting or replacing a linefeed (LF) with sed on Unix or Linux ==
== A note about deleting or replacing a linefeed (LF) with sed on Unix or Linux ==
Use the following syntax if you do not want to delete <code>\n</code> (new line):
Use the following syntax if you do not want to delete <code>\n</code> (new line):
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
<syntaxhighlight lang="shell" line="1" class="code-continue">
sed ':a;N;$!ba;s/\n//g' input-file -i.bak
sed ':a;N;$!ba;s/\n//g' input-file -i.bak
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 08:29, 26 September 2022

Ref­er­ences

Delete a car­riage re­turn (CR) with Sed com­mand

The sub­sti­tute com­mand syn­tax is as fol­lows (to get ^M type CTRL+V fol­lowed by CTRL+M i.e. don’t just type the carat sym­bol and a cap­i­tal M. It will not work):

sed -e 's/^M//g' input-file
sed -e 's/^M//g' input-file > output-file
# GNU/sed syntax
sed -i 's/^M//g' input
# Replace it with FOO
sed -i -e 's/^M/FOO/g' input

Or easy to use sed syn­tax to re­move car­riage re­turn in Unix or Lin­ux:

sed 's/\r$//' input-file > output-file
sed 's/\r$//g' input-file > output-file
# GNU/sed syntax
sed -i 's/\r$//g' input

To re­place a car­riage re­turn (CR) with sed com­mand – the syn­tax is:

sed 's/\r/YOUR-replacement-TEXT-HERE/' input-file > output-file
sed 's/\r/YOUR-replacement-TEXT-HERE/g' input-file > output-file
sed 's/\r/foo/g' input-file > output-file

How to ver­i­fy ^M in a text file by Cat com­mand

Use the cat com­mand as fol­low:

cat -v input-file

A note about delet­ing or re­plac­ing a line­feed (LF) with sed on Unix or Lin­ux

Use the fol­low­ing syn­tax if you do not want to delete \n (new line):

sed ':a;N;$!ba;s/\n//g' input-file -i.bak
sed ':a;N;$!ba;s/\n//g' input-file > output-file

Re­move a car­riage re­turn with dos2unix com­mand

You can al­so use dos2unix com­mand to con­verts text files from the DOS for­mat to the Unix for­mat:

dos2unix input-file
dos2unix -b input-file

The tr com­mand syn­tax

To delete a CRLF:

tr -d '\r' < input-file > output-file