应用环境:shell脚本使用工具
选项参数 | 功能 |
---|---|
-f | 列号,提取第几列 |
-d | 分隔符,按照指定分隔符分割列 |
切第一列
[shao@localhost shell]$ cut -d ":" -f 1 cut.txt
dong
guan
wo
shao
lai
jia
切第二列
[shao@localhost shell]$ cut -d ":" -f 2 cut.txt
shen
zhen
wo
yolo
le
li
切出yolo(grep过滤出行)
[shao@localhost shell]$ cat cut.txt | grep yolo | cut -d ":" -f 2
yolo
[shao@localhost shell]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/shao/.local/bin:/home/shao/bin
[shao@localhost shell]$ echo $PATH | cut -d ":" -f 3-
/usr/local/sbin:/usr/sbin:/home/shao/.local/bin:/home/shao/bin
[shao@localhost shell]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.25.129 netmask 255.255.255.0 broadcast 192.168.25.255
inet6 fe80::9791:3158:a215:71fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:34:5e:c3 txqueuelen 1000 (Ethernet)
RX packets 11541 bytes 893843 (872.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3454 bytes 391428 (382.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[shao@localhost shell]$ ifconfig ens33 | grep "netmask" | cut -d " " -f 10
192.168.25.129
选项参数 | 功能 |
---|---|
-e | 直接在指令列模式上进行sed的动作编辑。 |
命令 | 功能描述 |
---|---|
a | 新增,a的后面可以接字串,在下一行出现 |
d | 删除 |
s | 查找并替换 |
[shao@localhost shell]$ cat sed.txt
dong:shen:wang
guan:zhen:li
wo:wo:wo
shao:yolo:one
lai:le:le
jia:li:ba
[shao@localhost shell]$ sed '2a sed:juli' sed.txt
dong:shen:wang
guan:zhen:li
sed:juli
wo:wo:wo
shao:yolo:one
lai:le:le
jia:li:ba
[shao@localhost shell]$ sed '/wo/d' sed.txt
dong:shen:wang
guan:zhen:li
shao:yolo:one
lai:le:le
jia:li:ba
[shao@localhost shell]$ sed 's/wo/ni/g' sed.txt
dong:shen:wang
guan:zhen:li
ni:ni:ni
shao:yolo:one
lai:le:le
jia:li:ba
[shao@localhost shell]$ sed -e '2d' -e 's/wo/ni/g' sed.txt
dong:shen:wang
ni:ni:ni
shao:yolo:one
lai:le:le
jia:li:ba
选项参数 | 功能 |
---|---|
-F | 指定输入文件折分隔符 |
-v | 赋值一个用户定义变量 |
举例文件:系统配置passwd文件(备份出来使用)
[shao@localhost shell]$ cat 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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sssd:x:998:995:User for sssd:/:/sbin/nologin
libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:996:993:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:992:989::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
unbound:x:991:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:990:986:User for geoclue:/var/lib/geoclue:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
shao:x:1000:1000:shao:/home/shao:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
[shao@localhost shell]$ awk -F ':' '/^root/{print $7}' passwd
/bin/bash
[shao@localhost shell]$ awk -F ':' '/^root/{print $1","$7}' passwd
root,/bin/bash
[shao@localhost shell]$ awk -F ':' 'BEGIN{print "user,shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user,shell
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
adm,/sbin/nologin
lp,/sbin/nologin
sync,/bin/sync
shutdown,/sbin/shutdown
halt,/sbin/halt
mail,/sbin/nologin
operator,/sbin/nologin
games,/sbin/nologin
ftp,/sbin/nologin
nobody,/sbin/nologin
systemd-network,/sbin/nologin
dbus,/sbin/nologin
polkitd,/sbin/nologin
sssd,/sbin/nologin
libstoragemgmt,/sbin/nologin
colord,/sbin/nologin
rpc,/sbin/nologin
gluster,/sbin/nologin
saslauth,/sbin/nologin
abrt,/sbin/nologin
setroubleshoot,/sbin/nologin
rtkit,/sbin/nologin
radvd,/sbin/nologin
chrony,/sbin/nologin
qemu,/sbin/nologin
unbound,/sbin/nologin
ntp,/sbin/nologin
tss,/sbin/nologin
usbmuxd,/sbin/nologin
geoclue,/sbin/nologin
pulse,/sbin/nologin
gdm,/sbin/nologin
saned,/sbin/nologin
rpcuser,/sbin/nologin
nfsnobody,/sbin/nologin
gnome-initial-setup,/sbin/nologin
sshd,/sbin/nologin
avahi,/sbin/nologin
postfix,/sbin/nologin
tcpdump,/sbin/nologin
shao,/bin/bash
mysql,/bin/bash
dahaige,/bin/zuishuai
[shao@localhost shell]$ awk -v i=1 -F ':' '{print $3+i}' passwd
1
2
3
4
5
6
7
8
9
12
13
15
100
变量 | 说明 |
---|---|
FILENAME | 文件名 |
NR | 已读的记录数 |
NF | 浏览记录的域的个数(切割后,列的个数) |
[shao@localhost shell]$ awk -F ':' '{print "name:"FILENAME",hang:"NR",lie:"NF}' passwd
name:passwd,hang:1,lie:7
name:passwd,hang:2,lie:7
name:passwd,hang:3,lie:7
name:passwd,hang:4,lie:7
name:passwd,hang:5,lie:7
name:passwd,hang:6,lie:7
name:passwd,hang:7,lie:7
name:passwd,hang:8,lie:7
[shao@localhost shell]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.25.129 netmask 255.255.255.0 broadcast 192.168.25.255
inet6 fe80::9791:3158:a215:71fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:34:5e:c3 txqueuelen 1000 (Ethernet)
RX packets 19573 bytes 3350863 (3.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6434 bytes 721760 (704.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[shao@localhost shell]$ ifconfig ens33 | grep "netmask" | awk -F 'inet' '{print $2}' | awk -F ' ' '{print $1}'
192.168.25.129
[shao@localhost shell]$ cat sed.txt
dong:shen:wang
guan:zhen:li
wo:wo:wo
shao:yolo:one
lai:le:le
jia:li:ba
[shao@localhost shell]$ awk '/^$/{print NR}' sed.txt
7
选项 | 说明 |
---|---|
-n | 依照数值的大小排序 |
-r | 以相反的顺序来排序 |
-t | 设置排序时所用的分隔字符 |
-k | 指定需要排序的列 |
[shao@localhost shell]$ cat sort.txt
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6
[shao@localhost shell]$ sort -t ":" -nrk 3 sort.txt
bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3
ss:30:1.6