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=" | <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=" | <syntaxhighlight lang="shell" class="code-continue"> | ||
# Replace it with FOO | # Replace it with FOO | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" line="1" class=" | <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=" | <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=" | <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
References
- Main source of the article: Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix on nixCraft
- Reference: bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory
- Reference: How do I convert between Unix and Windows text files?
Delete a carriage return (CR) with Sed command
The substitute command syntax is as follows (to get ^M
type CTRL+V followed by CTRL+M i.e. don’t just type the carat symbol and a capital 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
syntax to remove carriage return in Unix or Linux:
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 replace a carriage return (CR) with sed
command – the syntax 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 verify ^M in a text file by Cat command
Use the cat command as follow:
cat -v input-file
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 \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
Remove a carriage return with dos2unix command
You can also use dos2unix
command to converts text files from the DOS format to the Unix format:
dos2unix input-file
dos2unix -b input-file
The tr command syntax
To delete a CRLF:
tr -d '\r' < input-file > output-file