Method of Deleting Enter Character from Text in Linux

  • 2021-06-28 14:41:48
  • OfStack

When the carriage return character ( Ctrl+M ) Don't worry when you get nervous.There are several simple ways to eliminate them.

The carriage return character goes back a long time - as early as on the typewriter there was a mechanism or lever that moved the holder of the paper drum to the right so that letters could be re-entered on the left.They retained it on text files on Windows, but never used it on the Linux system.This incompatibility can sometimes cause problems when you try to process files created on Windows on Linux, but it is a very easy problem to solve.

If you use od (8ary dump octal dump) command to view the file, then return (also used Ctrl+M Represents) Characters will be displayed as 15 of 8 digits.character CRLF Commonly used to represent a sequence of carriage return and line break characters at the end of a line in an Windows text file.Those who notice the octal dump will see \r\n .By contrast, the Linux text ends with a line break only.

There is one od Output example, highlighting rows in CRLF Character, and its octal digit.


$ od -bc testfile.txt
0000000 124 150 151 163 040 151 163 040 141 040 164 145 163 164 040 146
  T h i s i s a t e s t f
0000020 151 154 145 040 146 162 157 155 040 127 151 156 144 157 167 163
  i l e f r o m W i n d o w s
0000040 056 015 012 111 164 047 163 040 144 151 146 146 145 162 145 156 <==
  . \r \n I t ' s d i f f e r e n <==
0000060 164 040 164 150 141 156 040 141 040 125 156 151 170 040 164 145
  t t h a n a U n i x t e
0000100 170 164 040 146 151 154 145 015 012 167 157 165 154 144 040 142 <==
  x t f i l e \r \n w o u l d b <==

Although these characters aren't a big problem, they can sometimes cause interference when you want to parse the text in a way that you don't want to encode for its existence.

Three ways to delete carriage return characters from text

Fortunately, there are several ways to easily delete carriage returns.There are three options:

dos2unix

You may have trouble installing, but dos2unix Perhaps the easiest way to convert Windows text to Unix/Linux text.A command with one parameter is OK.No second file name is required.The file will be changed directly.


$ dos2unix testfile.txt
dos2unix: converting file testfile.txt to Unix format...

You should find that the file length decreases, depending on the number of lines it contains.Files with 100 lines may be reduced by 99 characters because only the last line will not CRLF End of character.

Before:


-rw-rw-r-- 1 shs shs 121 Sep 14 19:11 testfile.txt

After:


-rw-rw-r-- 1 shs shs 118 Sep 14 19:12 testfile.txt

If you need to convert a large number of files, you don't need to repair one at a time.Instead, put them all in a directory and run the following command:


$ find . -type f -exec dos2unix {} \;

In this command, we use find Find regular files and run dos2unix Command converts 1 at a time.In command {} Will be replaced with file name.At runtime, you should be in the directory that contains the files.This command can corrupt other types of files, such as files containing 8bit 15 in the context (for example, bytes in mirrored files) in addition to text files.

sed

You can also use the stream editor sed To delete the carriage return.However, you must provide a second file name.The following are examples:


$ sed -e  " s/^M// "  before.txt > after.txt

One important thing you need is that don't enter the characters you see.You must press Ctrl+V Heel Ctrl+M To enter ^M . s Is a replacement command.Slash turns the text we're looking for ( Ctrl + M Separate the text to be replaced (empty here).

vi

You can even use vi Delete carriage return ( Ctrl+M ), but let's assume you haven't opened hundreds of files, or maybe you're making some other modifications.You can type : Enter the command line and enter the string below.and sed 1, in command ^M Need to pass Ctrl+V input ^ And then Ctrl+M insert M . %s The slash separates the character we want to delete from the text we want to replace (empty) again. g (global) means to execute on all lines.


:%s/^M//g

summary

dos2unix Commands are probably the easiest to remember and the most reliable way to remove carriage returns from your text.The other options are a bit difficult to use, but they provide the same basic functionality.

via: https://www.networkworld.com/article/3438857/how-to-remove-carriage-returns-from-text-files-on-linux.html

summary


Related articles: