码迷,mamicode.com
首页 > 其他好文 > 详细

字符转换指令tr,col,expand

时间:2017-08-28 21:52:20      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:linux   tr   col   expand   

tr -[dsx] seting ...

使用tr替换字符:

[whx@localhost ~]$ last | head -n 5 | tr‘[a-z]‘ ‘[A-Z]‘
WHX     PTS/0        192.168.21.1     SUN AUG 27 18:22   STILL LOGGED IN  
REBOOT  SYSTEM BOOT  2.6.32-696.EL6.X SUNAUG 27 18:21 - 00:39  (06:18)   
WHX     PTS/0        192.168.21.1     THU AUG 24 18:37 - 02:54  (08:17)   
WHX     TTY1         :0               THU AUG 24 18:36 - DOWN   (08:18)   
REBOOT  SYSTEM BOOT  2.6.32-696.EL6.X THUAUG 24 18:35 - 02:55  (08:19)   
[whx@localhost ~]$ last | head -n 5
whx     pts/0        192.168.21.1     Sun Aug 27 18:22   still logged in  
reboot  system boot  2.6.32-696.el6.x SunAug 27 18:21 - 00:39  (06:18)   
whx     pts/0        192.168.21.1     Thu Aug 24 18:37 - 02:54  (08:17)   
whx     tty1         :0               Thu Aug 24 18:36 - down   (08:18)   
reboot  system boot  2.6.32-696.el6.x ThuAug 24 18:35 - 02:55  (08:19)

 

添加-d参数删除字符:

[whx@localhost ~]$ cat /etc/passwd |head -n5
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[whx@localhost ~]$ cat /etc/passwd |head -n5 |tr -d ‘:‘
rootx00root/root/bin/bash
binx11bin/bin/sbin/nologin
daemonx22daemon/sbin/sbin/nologin
admx34adm/var/adm/sbin/nologin
lpx47lp/var/spool/lpd/sbin/nologin

 

添加-s参数去除重复字符:

[whx@localhost ~]$ echo ‘thissss   issss aaaa test‘|tr -s ‘sa‘
this  is a  test

 

col常用于将man page转存为纯文本文件以方便查阅。

col -x 将tab替换成对等的空格键

[whx@localhost ~]$ cat  /etc/man.config | tail -10 | col -x |cat -A|tail -10
#$
# Enable/disable makewhatis database cronupdates.$
# If MAKEWHATISDBUPDATES variable isuncommented$
# and set to n or N, cron scripts$
# /etc/cron.daily/makewhatis.cron$
# /etc/cron.weekly/makewhatis.cron$
# will not update makewhatis database.$
# Otherwise the database will be updated.$
#$
#MAKEWHATISDBUPDATES    n$
[whx@localhost ~]$ cat  /etc/man.config | tail -10 | cat -A
#$
# Enable/disable makewhatis database cronupdates.$
# If MAKEWHATISDBUPDATES variable isuncommented$
# and set to n or N, cron scripts $
# /etc/cron.daily/makewhatis.cron$
# /etc/cron.weekly/makewhatis.cron$
# will not update makewhatis database.$
# Otherwise the database will be updated.$
# $
#MAKEWHATISDBUPDATES^In$

 

expand转换指令:

expand  [-t] file 

使用-t参数设置使用6个空格键代替tab键(默认使用8个空格键代替tab键)

[whx@localhost ~]$ grep ‘^MANPATH‘/etc/man.config |head -n 3 | expand -t 6 |cat -A
MANPATH    /usr/man$
MANPATH    /usr/share/man$
MANPATH    /usr/local/man$
[whx@localhost ~]$ grep ‘^MANPATH‘/etc/man.config |head -n 3 | cat -A
MANPATH^I/usr/man$
MANPATH^I/usr/share/man$
MANPATH^I/usr/local/man$


本文出自 “天黑顺路” 博客,请务必保留此出处http://mjal01.blog.51cto.com/12140495/1960194

字符转换指令tr,col,expand

标签:linux   tr   col   expand   

原文地址:http://mjal01.blog.51cto.com/12140495/1960194

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!