linux系统中如何删除^M

1、测试数据

[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10^M$
dddd^I33333^Icdddd^M$
dddd^I11111^I55555^M$

2、dos2unix删除

[root@centos79 test]# cat a.txt
SUN08   SUN09   SUN10
dddd    33333   cdddd
dddd    11111   55555
[root@centos79 test]# dos2unix a.txt
-bash: /usr/bin/dos2unix: No such file or directory
[root@centos79 test]# dnf install dos2unix -y
Last metadata expiration check: 1:51:03 ago on Sun 18 Jul 2021 09:26:03 AM CST.
Dependencies resolved.
=====================================================================================================================================
 Package                         Arch                          Version                             Repository                   Size
=====================================================================================================================================
Installing:
 dos2unix                        x86_64                        6.0.3-7.el7                         base                         74 k

Transaction Summary
=====================================================================================================================================
Install  1 Package

Total download size: 74 k
Installed size: 190 k
Downloading Packages:
dos2unix-6.0.3-7.el7.x86_64.rpm                                                                      126 kB/s |  74 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                 50 kB/s |  74 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                             1/1
  Installing       : dos2unix-6.0.3-7.el7.x86_64                                                                                 1/1
  Verifying        : dos2unix-6.0.3-7.el7.x86_64                                                                                 1/1

Installed:
  dos2unix-6.0.3-7.el7.x86_64

Complete!
[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10^M$
dddd^I33333^Icdddd^M$
dddd^I11111^I55555^M$
[root@centos79 test]# dos2unix a.txt
dos2unix: converting file a.txt to Unix format ...
[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10$
dddd^I33333^Icdddd$
dddd^I11111^I55555$

3、sed命令删除

[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10^M$
aaaaa^Ieeeee^Ikkkkk^M$
aaaaa^Iddddd^I88888^M$
[root@centos79 test]# sed 's/^M//g' a.txt -i
[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10$
aaaaa^Ieeeee^Ikkkkk$
aaaaa^Iddddd^I88888$

4、vi/vim命令删除

命令模式下输入:1,$ s/^M//g

5、tr删除

[root@centos79 test]# cat -A a.txt
SUN08^ISUN09^ISUN10^M$
aaaaa^Ieeeee^Ikkkkk^M$
aaaaa^Iddddd^I88888^M$
[root@centos79 test]# cat a.txt | tr -d "
" | cat -A
SUN08^ISUN09^ISUN10$
aaaaa^Ieeeee^Ikkkkk$
aaaaa^Iddddd^I88888$
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15025408.html