1. 程式人生 > >字符轉換指令tr,col,expand

字符轉換指令tr,col,expand

linux tr col expand

tr -[dsx] seting ...

使用tr替換字符:

[[email protected] ~]$ 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)   
[[email protected] ~]$ 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參數刪除字符:

[[email protected] ~]$ 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
[[email protected] ~]$ 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參數去除重復字符:

[[email protected] ~]$ echo ‘thissss   issss aaaa test‘|tr -s ‘sa‘
this  is a  test

col常用於將man page轉存為純文本文件以方便查閱。

col -x 將tab替換成對等的空格鍵

[[email protected] ~]$ 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$
[[email protected] ~]$ 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鍵)

[[email protected] ~]$ grep ‘^MANPATH‘/etc/man.config |head -n 3 | expand -t 6 |cat -A
MANPATH    /usr/man$
MANPATH    /usr/share/man$
MANPATH    /usr/local/man$
[[email protected] ~]$ 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