linux中如何删除^M符号

2022/1/6 7:04:47

本文主要是介绍linux中如何删除^M符号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、测试数据

root@DESKTOP-1N42TVH:/home/test2# ls    
outcome.map
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map  ## 末尾多出^M
1^Is64199.1^I0^I55910^M$
1^IOAR19_64675012.1^I0^I85204^M$
1^IOAR19_64715327.1^I0^I122948^M$
1^IOAR19_64803054.1^I0^I203750^M$
1^IDU281551_498.1^I0^I312707^M$
root@DESKTOP-1N42TVH:/home/test2# sed -n l outcome.map  ## \r为windows中换行符,\r等价于^M
1\ts64199.1\t0\t55910\r$
1\tOAR19_64675012.1\t0\t85204\r$
1\tOAR19_64715327.1\t0\t122948\r$
1\tOAR19_64803054.1\t0\t203750\r$
1\tDU281551_498.1\t0\t312707\r$

 

2、sed去除^M

root@DESKTOP-1N42TVH:/home/test2# ls
outcome.map
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map
1^Is64199.1^I0^I55910^M$
1^IOAR19_64675012.1^I0^I85204^M$
1^IOAR19_64715327.1^I0^I122948^M$
1^IOAR19_64803054.1^I0^I203750^M$
1^IDU281551_498.1^I0^I312707^M$
root@DESKTOP-1N42TVH:/home/test2# sed 's/^M//' outcome.map | cat -A  ##利用sed直接将^M替换为空,有的时候此方法不灵
1^Is64199.1^I0^I55910$
1^IOAR19_64675012.1^I0^I85204$
1^IOAR19_64715327.1^I0^I122948$
1^IOAR19_64803054.1^I0^I203750$
1^IDU281551_498.1^I0^I312707$
root@DESKTOP-1N42TVH:/home/test2# sed 's/\r//' outcome.map | cat -A   ## 利用sed直接替换掉\r
1^Is64199.1^I0^I55910$
1^IOAR19_64675012.1^I0^I85204$
1^IOAR19_64715327.1^I0^I122948$
1^IOAR19_64803054.1^I0^I203750$
1^IDU281551_498.1^I0^I312707$

 

3、利用tr删除掉^M

root@DESKTOP-1N42TVH:/home/test2# ls
outcome.map
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map
1^Is64199.1^I0^I55910^M$
1^IOAR19_64675012.1^I0^I85204^M$
1^IOAR19_64715327.1^I0^I122948^M$
1^IOAR19_64803054.1^I0^I203750^M$
1^IDU281551_498.1^I0^I312707^M$
root@DESKTOP-1N42TVH:/home/test2# sed -n l outcome.map
1\ts64199.1\t0\t55910\r$
1\tOAR19_64675012.1\t0\t85204\r$
1\tOAR19_64715327.1\t0\t122948\r$
1\tOAR19_64803054.1\t0\t203750\r$
1\tDU281551_498.1\t0\t312707\r$
root@DESKTOP-1N42TVH:/home/test2# cat outcome.map | tr -d "^M" | cat -A   ##直接删除掉^M
1^Is64199.1^I0^I55910$
1^IOAR19_64675012.1^I0^I85204$
1^IOAR19_64715327.1^I0^I122948$
1^IOAR19_64803054.1^I0^I203750$
1^IDU281551_498.1^I0^I312707$
root@DESKTOP-1N42TVH:/home/test2# cat outcome.map | tr -d "\r" | cat -A    ##直接删除掉\r
1^Is64199.1^I0^I55910$
1^IOAR19_64675012.1^I0^I85204$
1^IOAR19_64715327.1^I0^I122948$
1^IOAR19_64803054.1^I0^I203750$
1^IDU281551_498.1^I0^I312707$

 

4、利用dos2unix直接删除

a、安装dos2unix

root@DESKTOP-1N42TVH:/home/test2# ls
outcome.map
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map
1^Is64199.1^I0^I55910^M$
1^IOAR19_64675012.1^I0^I85204^M$
1^IOAR19_64715327.1^I0^I122948^M$
1^IOAR19_64803054.1^I0^I203750^M$
1^IDU281551_498.1^I0^I312707^M$
root@DESKTOP-1N42TVH:/home/test2# dos2unix outcome.map
-bash: /usr/bin/dos2unix: No such file or directory
root@DESKTOP-1N42TVH:/home/test2# apt install dos2unix   ## 安装dos2unix(ubuntu中)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  java-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2 liblcms2-2 libpcsclite1
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
  dos2unix
0 upgraded, 1 newly installed, 0 to remove and 84 not upgraded.
Need to get 374 kB of archives.
After this operation, 1342 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 dos2unix amd64 7.4.0-2 [374 kB]
Fetched 374 kB in 3s (137 kB/s)
Selecting previously unselected package dos2unix.
(Reading database ... 59750 files and directories currently installed.)
Preparing to unpack .../dos2unix_7.4.0-2_amd64.deb ...
Unpacking dos2unix (7.4.0-2) ...
Setting up dos2unix (7.4.0-2) ...
Processing triggers for man-db (2.9.1-1) ...
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map
1^Is64199.1^I0^I55910^M$
1^IOAR19_64675012.1^I0^I85204^M$
1^IOAR19_64715327.1^I0^I122948^M$
1^IOAR19_64803054.1^I0^I203750^M$
1^IDU281551_498.1^I0^I312707^M$
root@DESKTOP-1N42TVH:/home/test2# dos2unix outcome.map   ## 从dos系统转换到unix系统,也可以逆向转换,使用unix2dos outcome.map
dos2unix: converting file outcome.map to Unix format...
root@DESKTOP-1N42TVH:/home/test2# ls
outcome.map
root@DESKTOP-1N42TVH:/home/test2# cat -A outcome.map
1^Is64199.1^I0^I55910$
1^IOAR19_64675012.1^I0^I85204$
1^IOAR19_64715327.1^I0^I122948$
1^IOAR19_64803054.1^I0^I203750$
1^IDU281551_498.1^I0^I312707$

 



这篇关于linux中如何删除^M符号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程