linux-字符替换
tr
##把输出的信息中的小写换成大写
[root@localhost tmp]# last | tr ‘[a-z]’ ‘[A-Z]’
ROOT PTS/0 :0 MON DEC 29 08:09 STILL LOGGED IN
ROOT :0 :0 MON DEC 29 08:08 STILL LOGGED IN
(UNKNOWN :0 :0 MON DEC 29 06:48 - 08:08 (01:20)
REBOOT SYSTEM BOOT 3.10.0-123.EL7.X MON DEC 29 14:47 - 08:21 (-6:-25)
ROOT PTS/0 :1 THU DEC 25 07:52 - 09:20 (01:28)
ROOT :1 :1 THU DEC 25 06:58 - 09:21 (02:22)
(UNKNOWN :1 :1 THU DEC 25 06:58 - 06:
##把输出的信息中的冒号(:)删除
[root@localhost tmp]# last | tr -d ‘:’
root pts/0 0 Mon Dec 29 0809 still logged in
root 0 0 Mon Dec 29 0808 still logged in
(unknown 0 0 Mon Dec 29 0648 - 0808 (0120)
reboot system boot 3.10.0-123.el7.x Mon Dec 29 1447 - 0823 (-6-24)
root pts/0 1 Thu Dec 25 0752 - 0920 (0128)
root 1 1 Thu Dec 25 0658 - 0921 (0222)
##复制文件并转换其编码
[root@localhost tmp]# cp /etc/passwd /tmp/passwd && \
unix2dos /tmp/passwd
##查看文件的编码
[root@localhost tmp]# file /etc/passwd /tmp/passwd
/etc/passwd: ASCII text
/tmp/passwd: ASCII text
[root@localhost tmp]#
[root@localhost tmp]# cat /tmp/passwd |tr -d ‘\r’ > /tmp/passwd.linux[root@localhost tmp]# ls -l /etc/passwd /tmp/passwd*
-rw-r–r–. 1 root root 2824 Dec 25 08:33 /etc/passwd
-rw-r–r–. 1 root root 2824 Dec 29 08:25 /tmp/passwd
-rw-r–r–. 1 root root 2824 Dec 29 08:31 /tmp/passwd.linux
[root@localhost tmp]#
col
##显示文件中的特殊字符
[root@localhost tmp]# cat-A /tmp/passwd
bash: cat-A: command not found…
[root@localhost tmp]# cat -A /tmp/passwd
root❌0:0:root:/root:/bin/bash$
bin❌1:1:bin:/bin:/sbin/nologin$
daemon❌2:2:daemon:/sbin:/sbin/nologin$
adm❌3:4:adm:/var/adm:/sbin/nologin$
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin$
##把输入的文件结果中的^I[tab]转成空白并输出
[root@localhost tmp]# cat /etc/man_db.conf |col -x|cat -A|more
#$
#$
This file is used by the man-db package to configure the man and cat p
aths.$
It is also used to provide a manpath for those without one by examinin
g$
their PATH environment variable. For details see the manpath(5) man pa
ge.$
#$
Lines beginning with `#’ are comments and are ignored. Any combination
of$
##把man page 转换为 /root/col.man 纯文本文档
[root@localhost tmp]# man col > /tmp/col.man
[root@localhost tmp]# vi /tmp/col.man
[root@localhost tmp]#
join
处理两个文件之间的数据
##把/etc/passwd /etc/shadow相关数据整合成一栏
[root@localhost tmp]# head -n 3 /etc/passwd /etc/shadow
==> /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
==> /etc/shadow
root: 6 6 6zoMJbbV16.vhU23Q$NsUMn8D8d6IaAsE5BNywpVZbS5ZvVkV11l7WhHZt8X82DZpsRu6o48pIpFcssK1wSCF6Sb4hYdp/lJSF5kGBh.:16412:0:99999:7:::
bin:*:16231:0:99999:7:::
daemon:*:16231:0:99999:7:::
[root@localhost tmp]#
##两份文档第一个字段相同者整合成一行
[root@localhost tmp]# join -t ‘:’ /etc/passwd /etc/shadow
root❌0:0:root:/root:/bin/bash: 6 6 6zoMJbbV16.vhU23Q$NsUMn8D8d6IaAsE5BNywpVZbS5ZvVkV11l7WhHZt8X82DZpsRu6o48pIpFcssK1wSCF6Sb4hYdp/lJSF5kGBh.:16412:0:99999:7:::
bin❌1:1:bin:/bin:/sbin/nologin:*:16231:0:99999:7:::
daemon❌2:2:daemon:/sbin:/sbin/nologin:*:16231:0:99999:7:::
adm❌3:4:adm:/var/adm:/sbin/nologin:*:16231:0:99999:7:::
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin:*:16231:0:99999:7:::
sync❌5:0:sync:/sbin:/bin/sync:*:16231:0:99999:7:::
shutdown❌6:0:shutdown:/sbin:/sbin/shutdown:*:16231:0:99999:7:::
halt❌7:0:halt:/sbin:/sbin/halt:*:16231:0:99999:7:::
[root@localhost tmp]# head -n 3 /etc/passwd /etc/group
==> /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
==> /etc/group
root❌0:
bin❌1:
daemon❌2:
[root@localhost tmp]#
[root@localhost tmp]# join -t ‘:’ -1 4 /etc/passwd -2 3 /etc/group
0:root❌0:root:/root:/bin/bash:root❌
1:bin❌1:bin:/bin:/sbin/nologin:bin❌
2:daemon❌2:daemon:/sbin:/sbin/nologin:daemon❌
4:adm❌3:adm:/var/adm:/sbin/nologin:adm❌
join: /etc/passwd:6: is not sorted: sync❌5:0:sync:/sbin:/bin/sync
7:lp❌4:lp:/var/spool/lpd:/sbin/nologin:lp❌
join: /etc/group:11: is not sorted: wheel❌10:
99:nobody❌99:Nobody:/:/sbin/nologin:nobody❌
81:dbus❌81:System message bus:/:/sbin/nologin:dbus❌
999:polkitd❌999:User for polkitd:/:/sbin/nologin:polkitd❌
[root@localhost tmp]#
paste
把两份文档相关的数据 写在一起并用[tab]隔开
[root@localhost tmp]# paste /etc/passwd /etc/shadow|head -n 3
root❌0:0:root:/root:/bin/bash root: 6 6 6zoMJbbV16.vhU23Q$NsUMn8D8d6IaAsE5BNywpVZbS5ZvVkV11l7WhHZt8X82DZpsRu6o48pIpFcssK1wSCF6Sb4hYdp/lJSF5kGBh.:16412:0:99999:7:::
bin❌1:1:bin:/bin:/sbin/nologin bin:*:16231:0:99999:7:::
daemon❌2:2:daemon:/sbin:/sbin/nologin daemon:*:16231:0:99999:7:::
[root@localhost tmp]#
expand
把tab转换成空格
[root@localhost tmp]# grep ‘jimi’ passwd | head -n 3
jimistephen❌1000:1000:jimistephen:/home/jimistephen:/bin/bash
jimi❌1001:1001:www.jimi.com:/home/jimi:/bin/bash
[root@localhost tmp]#
##一个tab换成 6个空格
[root@localhost tmp]# grep ‘jimi’ passwd |head -n 3| expand -t 6 -| \
cat -A
jimistephen❌1000:1000:jimistephen:/home/jimistephen:/bin/bash$
jimi❌1001:1001:www.jimi.com:/home/jimi:/bin/bash$
[root@localhost tmp]#
split 分割文档
##每10条记录记录成一个文件
[root@localhost tmp]# ls -al /| split -l 10 - lsroot
[root@localhost tmp]# wc -l lsroot*
10 lsrootaa
10 lsrootab
4 lsrootac
24 total
[root@localhost tmp]#
xargs 参数的切换