tr 命令

转换和删除字符

选项:
-d --delete:删除字符
-s --squeeze-repeats:把连续重复的字符以一个字符表示,即去重
-c –C --complement:取字符集的补集 该命令会把/etc/issue中的小写字符都转换成大写字符
tr 'a-z' 'A-Z' < /etc/issue
删除fstab文件中的所有abc中任意字符
tr –d abc < /etc/fstab
将df命令输出的结果中为连续重复的空格,用一个空格取代
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 16603136 7635296 8967840 46% /
devtmpfs 485144 0 485144 0% /dev
tmpfs 500664 140 500524 1% /dev/shm
tmpfs 500664 26076 474588 6% /run
tmpfs 500664 0 500664 0% /sys/fs/cgroup
/dev/mapper/centos-home 2037760 33000 2004760 2% /home
/dev/sda1 201388 145376 56012 73% /boot
tmpfs 100136 36 100100 1% /run/user/0
[root@localhost ~]# df > df.log
[root@localhost ~]# tr -s ' ' < df.log
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 16603136 7635292 8967844 46% /
devtmpfs 485144 0 485144 0% /dev
tmpfs 500664 140 500524 1% /dev/shm
tmpfs 500664 26076 474588 6% /run
tmpfs 500664 0 500664 0% /sys/fs/cgroup
/dev/mapper/centos-home 2037760 33000 2004760 2% /home
/dev/sda1 201388 145376 56012 73% /boot
tmpfs 100136 36 100100 1% /run/user/0
[root@localhost ~]# tr -s ' ' : < df.log
Filesystem:1K-blocks:Used:Available:Use%:Mounted:on
/dev/mapper/centos-root:16603136:7635292:8967844:46%:/
devtmpfs:485144:0:485144:0%:/dev
tmpfs:500664:140:500524:1%:/dev/shm
tmpfs:500664:26076:474588:6%:/run
tmpfs:500664:0:500664:0%:/sys/fs/cgroup
/dev/mapper/centos-home:2037760:33000:2004760:2%:/home
/dev/sda1:201388:145376:56012:73%:/boot
tmpfs:100136:36:100100:1%:/run/user/0 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 ~]# hexdump -C windows.txt
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007 [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 ~]# hexdump -C linux.txt
00000000 61 0a 62 0a 63 |a.b.c|
00000005 [root@localhost ~]# cat linux.txt
a
b
c 注意:不能使用 tr 命令将文件从 Unix 格式转换为 Windows(DOS)。 除此之外Linux还提供了两种文本格式相互转化的命令:dos2unix和unix2dos,dos2unix把"\r\n"转化成"\n",unix2dos把"\n"转化成"\r\n"。

linux tr命令实现windows文本格式与linux文本格式间的转换的更多相关文章

  1. (转)linux traceroute命令参数及用法详解--linux跟踪路由命令

    linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...

  2. linux scp命令参数及用法详解--linux远程复制拷贝命令使用实例【转】

    转自:http://blog.csdn.net/jiangkai_nju/article/details/7338177 一般情况,本地网络跟远程网络进行数据交抱,或者数据迁移,常用的有三种方法,一是 ...

  3. Linux常用命令英文全称与中文解释Linux系统

    Linux常用命令英文全称与中文解释Linux系统(转)   Linux常用命令英文全称与中文解释Linux系统 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Pri ...

  4. linux dmesg命令参数及用法详解(linux显示开机信息命令)

    linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...

  5. linux tr命令详解

    通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符.您也可以用它来除去重复 ...

  6. [转]linux tr命令详解

    转自:http://www.cnblogs.com/huangxingkezhan/archive/2013/01/23/2874031.html 通过使用 tr,您可以非常容易地实现 sed 的许多 ...

  7. Linux tr命令

    介绍 tr命令可以对来自标准输入的字符进行替换.压缩和删除.tr只能接收来自标准的输入流,不能接收参数. 语法 tr [OPTION]... SET1 [SET2] 注意:SET2是可选项 OPTIO ...

  8. Linux tr 命令使用

    man tr: TR(1) User Commands TR(1) NAME tr - translate or delete characters SYNOPSIS tr [OPTION]... S ...

  9. Linux tr命令使用方法

    tr命令主要用于删除文件中控制字符或进行字符转换.本文主要介绍tr命令的基本语法和使用实例. tr基本语法 tr命令格式:tr [ -d ] [ -c ] [ -s ] [ 字符串1 ] [ 字符串2 ...

随机推荐

  1. 为CentOS 6、7升级gcc至4.8、4.9、5.2、6.3、7.3等高版本

    CentOS 7虽然已经出了很多年了,但依然会有很多人选择安装CentOS 6,CentOS 6有些依赖包和软件都比较老旧,如今天的主角gcc编译器,CentOS 6的gcc版本为4.4,CentOS ...

  2. 学习JDBC遇到的一些问题

    1. 数据库版本与驱动对应问题 参考官方文档:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-versions.html 具体详情还需 ...

  3. 淘大大出了composer镜像 -- 给力

    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

  4. PHP页面编码问题

    页面编码统一MySQL数据库编码.html页面编码.PHP或html文件本身编码要全部一致.1.MySQL数据库编码:建立数据库时指定编码(如gbk_chinese_ci),建立数据表.建立字段.插入 ...

  5. 关于 BSGS 以及 ExBSGS 算法的理解

    BSGS 引入 求解关于\(X\)的方程, \[A^X\equiv B \pmod P \] 其中\(Gcd(A,P)=1\) 求解 我们令\(X=i*\sqrt{P}-j\),其中\(0<=i ...

  6. 《PHP程序员面试笔试宝典》——如何应对面试官的“激将法”语言?

    如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> "激将法"是面试官用以淘汰求职者的一种惯用方法,它是指面试官采用怀疑.尖锐或咄咄逼人的交流方式来对求 ...

  7. yum 搭建私有仓库

    今日内容 Linux 中安装软件的三种方法 yum 私有仓库 selinux 和 firewalld (iprables) 解决系统乱码 内容详细 一.Linux 安装软件的三种方法 rpm安装.yu ...

  8. 我们一起来学Shell - shell的函数

    文章目录 定义函数 执行不带参数的函数 执行带参数的函数 函数的执行总结 我们一起来学Shell - 初识shell 我们一起来学Shell - shell的变量 我们一起来学Shell - shel ...

  9. Java老码农心得:卷了这么多年,您真的卷会了吗?

    前言 大家好,我是福隆苑居士,今天跟大家聊一下程序员在当下内卷成风的情况下,使用什么方法可以了解行业发展趋势,知道哪些该学,哪些可以略过,今年应该掌握什么,可以放弃什么,让自己时刻紧跟行业的步伐永不掉 ...

  10. yum配置及使用命令

    linux yum 命令 yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. 基於RPM包管理,能够从指 ...