微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux tr命令实现windows文本格式与linux文本格式间的转换

tr 命令
转换和删除字符

选项:
-d --delete:删除字符
-s --squeeze-repeats:把连续重复的字符以一个字符表示,即去重

该命令会把/etc/issue中的小写字符都转换成大写字符
tr ‘a-z’ ‘A-Z’< /etc/issue
删除fstab文件中的所有abc中任意字符
tr –d abc < /etc/fstab

windows文本格式与linux文本格式间的转换,windows格式文本中比linux格式文本中多回车键’\r’,通过tr删除’\r’实现格式转换
实例如下:
[root@localhost ~]# cat windows.txt
a
b
c

[root@localhost ~]# file windows.txt
windows.txt: ASCII text, with CRLF line terminators
[root@localhost ~]# hexdump windows.txt 
0000000 0d61 620a 0a0d 0063                    
0000007

[root@localhost ~]# tr -d '\r' <windows.txt >linux.txt

[root@localhost ~]# file linux.txt
linux.txt: ASCII text
[root@localhost ~]# hexdump linux.txt 
0000000 0a61 0a62 0063                         
0000005

[root@localhost ~]# cat linux.txt
a
b
c

注意:不能使用 tr 命令将文件从 Unix 格式转换为 Windows(DOS)。

除此之外Linux还提供了两种文本格式相互转化的命令:dos2unix和unix2dos,dos2unix把"\r\n"转化成"\n",unix2dos把"\n"转化成"\r\n"。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐