grep、cut

grep

行过滤工具
grep [选项] ‘关键字’ 文件名
grep -rn “xxx” filename
-n 显示行号
-i 不区分大小写
-v 取反
-r 遍历目录查找
grep -n ‘^root’ filename //root开头
grep -n ‘root$’ filename //root结尾
使用场景(需要查看上下文时)
grep -nA 3 ‘root$’ filename //显示root结尾的后3行 A:after
grep -nB 3 ‘root$’ filename //显示root结尾的前3行 B:before
grep -nC 3 ‘root$’ filename //显示root结尾的前后3行 C:context

将找到关键词部分加上颜色显示
grep --color=auto -rn “xxx” filename

在这里插入图片描述
在这里插入图片描述
将–color=auto加入别名
临时设置:

 alias grep='grep  --color=auto' //只针对当前用户和当前终端有效

永久设置:

1、把alias grep='grep  --color=auto'复制黏贴到/etc/bashrc最后一行
2、重新读取下: source /etc/bashrc

切换用户

su - 用户名

查看文本前十行

head filename

清屏

ctrl+l

cut

列截取工具
在这里插入图片描述

cut -d: -f1 /etc/passwd  //以:为分隔符,截取第一列内容
cut -d: -f1,7 /etc/passwd|head  //以:为分隔符,截取第1和第7列的前十行内容
cut -c1-5 /etc/passwd  //截取前5个字符的列
cut -c10- /etc/passwd  //截取第10个字符以后的列

列出当前系统的运行级别

/sbin/runlevel | cut -d ' ' -f2

运行级别配置文件

cat /etc/init/rc-sysinit.conf 

参考文章

发布了49 篇原创文章 · 获赞 3 · 访问量 2069

猜你喜欢

转载自blog.csdn.net/darling_user/article/details/104371301
cut