一、AWK简单命令
1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符
root@servera sed]# sed 's/^[[:space:]]//' grub2.conf
# these have execstack, and break under selinux
-b /usr/bin/grub2-script-check
-b /usr/bin/grub2-mkrelpath
-b /usr/bin/grub2-fstest
-b /usr/sbin/grub2-bios-setup
-b /usr/sbin/grub2-probe
-b /usr/sbin/grub2-sparc64-setup
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[root@servera sed]# sed 's/^#[[:space:]]*//' fstab
/etc/fstab
Created by anaconda on Wed Jun 1 13:14:18 2022
Accessible filesystems, by reference, are maintained under '/dev/disk/'.
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=03cbb3fe-dbd3-4721-8b02-d023983101af /boot xfs defaults 0 0
/dev/mapper/rhel-swap none swap defaults 0 0
3、在/root/install.log每一行行首增加#号
[root@servera sed]# sed 's/^.*$/#/' /root/install.log
4、在/etc/fstab文件中不以#开头的行的行首增加#号
root@servera sed]# sed 's/^[^#]/#&/' fstab
#
# /etc/fstab
# Created by anaconda on Wed Jun 1 13:14:18 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#/dev/mapper/rhel-root / xfs defaults 0 0
#UUID=03cbb3fe-dbd3-4721-8b02-d023983101af /boot xfs defaults 0 0
#/dev/mapper/rhel-swap none swap defaults 0 0
[root@servera sed]#
5、利用sed取出ifconfig命令中本机的IPv4地址
[root@servera sed]# ifconfig | sed -n '2p' | sed -r "s/.*inet[[:space:]]*//" | sed -r "s/[[:space:]]*netmask.*//"
192.168.10.135
-r: 读入文件内容追加到匹配行后面
6、关闭本机SELinux的功能
[root@servera sed]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@servera sed]# systemctl is-active selinux
inactive
-i: 直接生效
7、在/etc/hosts配置文件中添加内容
[root@servera sed]# sed 's/$/serverc/g' /etc/hosts
192.168.10.135 serveraserverc
192.168.10.136 serverbserverc
$:在行后添加内容,g:全局有效
[root@servera sed]# sed 's/^/serverc/g' /etc/hosts
serverc192.168.10.135 servera
serverc192.168.10.136 serverb
^:在行首添加内容,g全局有效
[root@servera sed]# sed '2 s/^/serverc/' /etc/hosts
192.168.10.135 servera
serverc192.168.10.136 serverb
2:在指定行(2)行首添加内容
二、AWK简单笔记
ield: 字段
NF: Number of fields 字段数量
NR: number of records:记录数量
$0: $0 is the whole record: 读入的这一行整个内容
$1,$2,$3...$N: 第几个字段
FS: field-separator 字段分隔符
RS: record-separator 记录分隔符
行和列的概念:
行:就是正常从文本文件中或者标准输入中读取的行
使用行分隔符来进行分割,把一行变成多行: RS
列:即我们对行使用FS进行分割的,来变成我们的列: FS
OFS: output field-separator: 使用awk处理完成之后的数据可以指定连接符,来连接我们的字段(列)
awk:
-F: --field-separator 字段分隔符:
root@localhost ~]# echo "1 2 3 4 5 6"|awk '{print $0}'
1 2 3 4 5 6
[root@localhost ~]# echo "1 2 3 4 5 6"|awk '{print $1}'
1
[root@localhost ~]# echo "1 2 3 4 5 6"|awk '{print $2}'
2
[root@localhost ~]# echo "1 2 3 4 5 6"|awk '{print $6}'
6
(以上是打印第一个字符)
[root@localhost ~]# echo "1:2:3:4:5:6"|awk '{print $6}'
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $6}'
6
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $3}'
3
(以上是用:分隔符分开之后,后面要用-F ":"区分出符号“之后才能识别打印分隔符)
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $NR}'
1
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $NF}'
6
(以上分别打印行数和列数)
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $1 $NF}'
16
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" '{print $1 "\t" $NF}'
1 6
(以上可以看出分别打印的是第一个字符和列数。用\t隔开,也可以使用逗号,隔开)
[root@localhost ~]# echo -e "1:2:3:4:5:6\n11:22:33:44:55:66"|awk -F ":" 'NR>=2 {print $2}'
22
(以上指令是NR>=2实在记录第二行是才输出第二个字符,所以前面的东西用了\n换行)
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" ' {print $2,$NF,NR}'
2 6 1
[root@localhost ~]# echo "1:2:3:4:5:6"|awk -F ":" ' {print $2,$NR,NF}'
2 1 6
(以上分别打印第二个字符,行数列数)