Linux文本处理命令主要包括以下几种:
[root@hanyw-bash-python ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# awk '/root/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# sed -n /root/p /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# sed -n "/root/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# cut -d: -f1 /etc/passwd | head
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@hanyw-bash-python ~]# sort /etc/passwd | head
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
boss:x:1002:1002:两组老板可查账号:/home/boss:/sbin/nologin
chrony:x:997:995:chrony system user:/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
flatpak:x:987:984:User for flatpak system helper:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
geoclue:x:989:986:User for geoclue:/var/lib/geoclue:/sbin/nologin
[root@hanyw-bash-python ~]# sort /etc/passwd | uniq -c | sort -rn | head
1 zabbix:x:992:992:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
1 yfyg:x:1000:1000:公司研发组研发员工账号:/home/yfyg:/sbin/nologin
1 xcyg:x:1001:1001:公司宣传组宣传员工账号:/home/xcyg:/sbin/nologin
1 www:x:1003:1003::/home/www:/sbin/nologin
1 tss:x:59:59:Account used for TPM access:/:/sbin/nologin
1 systemd-oom:x:993:993:systemd Userspace OOM Killer:/:/usr/sbin/nologin
1 systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
1 sync:x:5:0:sync:/sbin:/bin/sync
1 sssd:x:998:996:User for sssd:/:/sbin/nologin
1 sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
[root@hanyw-bash-python ~]# wc < /etc/passwd
32 68 1717
tr命令用于字符转换和删除。以下是一些例子:
将小写字母转换为大写字母:
[root@hanyw-bash-python ~]# echo "hello world" | tr 'a-z' 'A-Z'
HELLO WORLD
删除字符串中的空格:
[root@hanyw-bash-python ~]# echo "hello world" | tr -d ' '
helloworld
将数字转换为对应的英文字母:
[root@hanyw-bash-python ~]# echo "1234567890" | tr '1234567890' 'abcdefghij'
abcdefghij
将多个字符替换为一个字符:
[root@hanyw-bash-python ~]# echo "hello world" | tr 'My name is hanyw' '*'
**llo**orld
[root@hanyw-bash-python ~]# head /etc/passwd
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@hanyw-bash-python ~]# head /etc/shadow
root:$6$m04vxfuOCpe/BEMs$hMLXhWnxjKLXsuN2dq97qfOiExucrI9N7OGWyAQajsA2AZmVTvc7haF9muP2YPioAGxh5vWuKX8skfWCEOp/G1::0:99999:7:::
bin:*:19347:0:99999:7:::
daemon:*:19347:0:99999:7:::
adm:*:19347:0:99999:7:::
lp:*:19347:0:99999:7:::
sync:*:19347:0:99999:7:::
shutdown:*:19347:0:99999:7:::
halt:*:19347:0:99999:7:::
mail:*:19347:0:99999:7:::
operator:*:19347:0:99999:7:::
[root@hanyw-bash-python ~]# paste -d':' /etc/passwd /etc/shadow > /opt/passwd-shadow.txt
[root@hanyw-bash-python ~]# cat /opt/passwd-shadow.txt
root:x:0:0:root:/root:/bin/bash:root:$6$m04vxfuOCpe/BEMs$hMLXhWnxjKLXsuN2dq97qfOiExucrI9N7OGWyAQajsA2AZmVTvc7haF9muP2YPioAGxh5vWuKX8skfWCEOp/G1::0:99999:7:::
bin:x:1:1:bin:/bin:/sbin/nologin:bin:*:19347:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:daemon:*:19347:0:99999:7:::
adm:x:3:4:adm:/var/adm:/sbin/nologin:adm:*:19347:0:99999:7:::
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin:lp:*:19347:0:99999:7:::
sync:x:5:0:sync:/sbin:/bin/sync:sync:*:19347:0:99999:7:::
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown:shutdown:*:19347:0:99999:7:::
halt:x:7:0:halt:/sbin:/sbin/halt:halt:*:19347:0:99999:7:::
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin:mail:*:19347:0:99999:7:::
operator:x:11:0:operator:/root:/sbin/nologin:operator:*:19347:0:99999:7:::
games:x:12:100:games:/usr/games:/sbin/nologin:games:*:19347:0:99999:7:::
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin:ftp:*:19347:0:99999:7:::
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin:nobody:*:19347:0:99999:7:::
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin:systemd-coredump:!!:19687::::::
dbus:x:81:81:System message bus:/:/sbin/nologin:dbus:!!:19687::::::
tss:x:59:59:Account used for TPM access:/:/sbin/nologin:tss:!!:19687::::::
sssd:x:998:996:User for sssd:/:/sbin/nologin:sssd:!!:19687::::::
chrony:x:997:995:chrony system user:/var/lib/chrony:/sbin/nologin:chrony:!!:19687::::::
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin:sshd:!!:19687::::::
systemd-oom:x:993:993:systemd Userspace OOM Killer:/:/usr/sbin/nologin:systemd-oom:!*:19687::::::
zabbix:x:992:992:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin:zabbix:!!:19690::::::
nginx:x:991:991::/home/nginx:/sbin/nologin:nginx:!!:19694::::::
yfyg:x:1000:1000:公司研发组研发员工账号:/home/yfyg:/sbin/nologin:yfyg:!!:19726:0:99999:7:::
xcyg:x:1001:1001:公司宣传组宣传员工账号:/home/xcyg:/sbin/nologin:xcyg:!!:19726:0:99999:7:::
boss:x:1002:1002:两组老板可查账号:/home/boss:/sbin/nologin:boss:!!:19726:0:99999:7:::
polkitd:x:990:987:User for polkitd:/:/sbin/nologin:polkitd:!!:19730::::::
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin:rtkit:!!:19730::::::
geoclue:x:989:986:User for geoclue:/var/lib/geoclue:/sbin/nologin:geoclue:!!:19730::::::
pipewire:x:988:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin:pipewire:!!:19730::::::
flatpak:x:987:984:User for flatpak system helper:/:/sbin/nologin:flatpak:!!:19730::::::
pesign:x:986:983:Group for the pesign signing daemon:/run/pesign:/sbin/nologin:pesign:!!:19730::::::
www:x:1003:1003::/home/www:/sbin/nologin:www:!!:19731:0:99999:7:::
[root@hanyw-bash-python ~]# tail /etc/passwd
yfyg:x:1000:1000:公司研发组研发员工账号:/home/yfyg:/sbin/nologin
xcyg:x:1001:1001:公司宣传组宣传员工账号:/home/xcyg:/sbin/nologin
boss:x:1002:1002:两组老板可查账号:/home/boss:/sbin/nologin
polkitd:x:990:987:User for polkitd:/:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
geoclue:x:989:986:User for geoclue:/var/lib/geoclue:/sbin/nologin
pipewire:x:988:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
flatpak:x:987:984:User for flatpak system helper:/:/sbin/nologin
pesign:x:986:983:Group for the pesign signing daemon:/run/pesign:/sbin/nologin
www:x:1003:1003::/home/www:/sbin/nologin