案例01:过滤出/etc/passwd 中包含root或nobody的行
egrep 'root|nobody' /etc/passwd sed -rn '/root|nobody/p' /etc/passwd awk '/root|nobody/' /etc/passwd sed -r '/root|nobody/!d' /etc/passwd
案例02:过滤出/etc/passwd 中以root开头的行
egrep '^root' /etc/passwd sed -n '/^root/p' /etc/passwd awk '/^root/' /etc/passwd sed '/^root/!d' /etc/passwd
案例03:在/etc/ssh/sshd_config 中过滤出包含permitrootlogin或usedns的行(不区分大小写)
egrep -i 'permitrootlogin|usedns' /etc/ssh/sshd_config # -i 忽略大小写 sed -r '/permitrootlogin|usedns/I!d' /etc/ssh/sshd_config # I!d 或略大小写
案例04:显示/etc/下面一层以.conf结尾的文件
ls /etc/*.config # 此处的*为通配符 ls /etc/ |egrep '\.conf$' # 此处的*为正则表达式 ls /etc/ |sed -rn '/.*\.conf$/p' ls /etc/ |awk '/.*\.conf$/' find /etc/ -maxdepth 1 -type f -name '*.conf'
案例05:使用grep取出/etc/passwd第一列数据(取出用户名)
egrep -o '^[a-Z]+' /etc/passwd awk -F ':' '{print $1}' /etc/passwd # -F ':':指定字段分隔符为冒号 (:) # {print $1}:打印每一行的第一个字段,即用户名 sed 's#:.*##g' /etc/passwd
案例06:查看文件中的内容
vi vim cat head tail less more grep sed awk
案例07:查看一个文件第一行到第三行内容
seq 3 10 |head -n 3 seq 3 10 |sed -n '1,3p' seq 3 10 |awk 'NR==1,NR==3'
案例08:找出/etc/下面所有以.con结尾的文件,然后过滤出包含linux的行
find /etc/ -type f -name '*.conf' |egrep 'linux' find /etc/ -type f -name '*.conf' |sed -n '/root/p' find /etc/ -type f -name '*.conf' |awk '/root/'
浙公网安备 33010602011771号