⽂本处理⼯具面试题
一级标题
⽂本处理⼯具面试题
1、找出ifconfig “⽹卡名” 命令结果中本机的IPv4地址#
[root@magedu ~]# ifconfig ens33 | grep netmask | tr -s " " ":"|cut -d: -f3
[root@magedu ~]# ifconfig ens33|head -n2|tail -n1|tr -s " " |cut -d" " -f3
2、查出分区空间使⽤率的最⼤百分⽐值
[root@magedu ~]# df | grep ^/dev | tr -s " " ":" | cut -d: -f5 |cut -d% -f1 | sort -nr |
head -n1
3、查出⽤户UID最⼤值的⽤户名、 UID及shell类型
[root@magedu ~]# cat /etc/passwd|cut -d: -f1,3,7|sort -nr -t":" -k2|head -1
4、查出/tmp的权限,以数字⽅式显⽰
[root@magedu ~]# stat /tmp |head -n4|tail -n1 |cut -d"/" -f1|cut -d"(" -f2
[root@magedu ~]# stat /tmp|head -n4|tail -1|cut -d: -f2|tr -dc "[:digit:]\n"
5、统计当前连接本机的每个远程主机IP的连接数,并按从⼤到⼩排序
[root@magedu ~]# netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c
1、显⽰/proc/meminfo⽂件中以⼤⼩s开头的⾏(要求:使⽤两种⽅法)
[root@magedu ~]# egrep ^[sS].* /proc/meminfo
[root@magedu ~]# grep -i ^s.* /proc/meminfo
2、显⽰/etc/passwd⽂件中不以/bin/bash结尾的⾏
[root@magedu ~]# grep -v "/bin/bash$" /etc/passwd
3、显⽰⽤户rpc默认的shell程序
[root@magedu ~]# grep '\brpc\b' /etc/passwd|cut -d: -f1,7
4、找出/etc/passwd中的两位或三位数
[root@magedu ~]# grep -o "\b[0-9]\{2,3\}\b" /etc/passwd | cat -n
5、显⽰CentOS7的/etc/grub2.cfg⽂件中,⾄少以⼀个空⽩字符开头的且后⾯有⾮空⽩字符的⾏
[root@magedu ~]# egrep ^[[:space:]]\+[^[:space:]].*$ /etc/grub2.cfg
6、找出“netstat -tan” 命令结果中以LISTEN后跟任意多个空⽩字符结尾的⾏
[root@magedu ~]# netstat -tan |egrep ".*LISTEN[[:space:]]+$"
7、显⽰CentOS7上所有系统⽤户的⽤户名和UID
[root@magedu ~]# cut -d: -f1,3 passwd |egrep "\b[0-9]{1,3}\b"
8、添加⽤户bash、 testbash、 basher、 sh、 nologin(其shell为/sbin/nologin),找出/etc/passwd⽤户名和shell同名
的⾏
cat /etc/passwd 查看已有用户
useradd bash
useradd testbash
useradd basher
useradd sh
useradd nologin
[root@magedu ~]# egrep "^(.*)(:.*)\b\1$" /etc/passwd
9、利⽤df和grep,取出磁盘各分区利⽤率,并从⼤到⼩排序
[root@magedu ~]# df |grep ^/dev/sd |tr -s " " ":"|cut -d: -f5 |cut -d% -f1 |sort -nr
1、显⽰三个⽤户root、 mage、 wang的UID和默认shell
[root@magedu ~]# egrep "^(root|mage|wang)" /etc/passwd|cut -d: -f1,3,7
2、找出/etc/rc.d/init.d/functions⽂件中⾏⾸为某单词(包括下划线)后⾯跟⼀个⼩括号的⾏
[root@magedu ~]# grep -E "^[[:alpha:]_]+\(\)" /etc/rc.d/init.d/functions
3、使⽤egrep取出/etc/rc.d/init.d/functions中其基名
[root@magedu ~]# echo /etc/rc.d/init.d/functions|egrep -o "[^/]+/?$"
4、使⽤egrep取出上⾯路径的⽬录名
[root@magedu ~]# echo /etc/rc.d/init.d/functions |egrep -o "/.*[^/]"|egrep -o "/.*/"
5、统计last命令中以root登录的每个主机IP地址登录次数
[root@magedu ~]# last | grep ^root | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | sort |uniq -c
6、利⽤扩展正则表达式分别表⽰0-9、 10-99、 100-199、 200-249、 250-255
[0-9]
[1-9][0-9]
1[0-9][0-9]
2[0-4][0-9]
25[0-5]
7、显⽰ifconfig命令结果中所有IPv4地址
[root@magedu ~]# ifconfig | egrep -o " inet (addr:)?([0-9]*\.){3}[0-9]*"|egrep -o "([0-
9]*\.){3}[0-9]*"
8、将此字符串: welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前⾯
[root@magedu ~]# echo "welcome to magedu linux" |grep -o "."|sort|uniq -c|sort -nr
1、复制/etc/profile⾄/tmp/⽬录,⽤查找替换命令删除/tmp/profile⽂件中的⾏⾸的空⽩字符
[root@magedu ~]# cp /etc/profile /tmp/
[root@magedu ~]# vim /tmp/profile
:%s/^[[:space:]]\+//g
2、复制/etc/rc.d/init.d/functions⽂件⾄/tmp⽬录,⽤查找替换命令为/tmp/functions的每⾏开头为空⽩字符的⾏的⾏
⾸添加⼀个#号
[root@magedu ~]# cp /etc/rc.d/init.d/functions /tmp
[root@magedu ~]# vim /tmp/functions
:%s/^[[:space:]]/#/g
3、在vim中设置tab缩进为4个字符
[root@magedu ~]# vim /etc/vimrc
set tabstop=4
4、复制/etc/rc.d/init.d/functions⽂件⾄/tmp⽬录,替换/tmp/functions⽂件中的/etc/sysconfig/init为/var/log
[root@magedu ~]# cp /etc/rc.d/init.d/functions /tmp
[root@magedu ~]# vim /tmp/functions
:%s@\/etc\/sysconfig\/init@\/var\/log@g
5、删除/tmp/functions⽂件中所有以#开头,且#后⾯⾄少有⼀个空⽩字符的⾏的⾏⾸的#号
[root@magedu ~]# vim /tmp/functions
:%s@^#\([[:space:]]\+.*\)@\1@g

浙公网安备 33010602011771号