取第二行
[root@cenos7 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.112.136 netmask 255.255.255.0 broadcast 192.168.112.255
inet6 fe80::1215:e4cd:7a24:6aa1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1d:ed:a8 txqueuelen 1000 (Ethernet)
RX packets 23701 bytes 2628782 (2.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 19102 bytes 4030633 (3.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@cenos7 ~]# ifconfig ens33 |head -n2 | tail -n1
inet 192.168.112.136 netmask 255.255.255.0 broadcast 192.168.112.255
取列
cut -d : -f1,3 /etc/passwd
取隨機(jī)數(shù) 只要數(shù)字加字母 取前10個(gè)
cat /dev/urandom
openssl rand 產(chǎn)生隨機(jī)數(shù)
openssl rand -base 64 12 \
cat /dev/urandom |tr -dc '[:alnum:]' | head -c10
取出IP地址
[root@cenos7 ~]# ifconfig ens33 cenos7
[root@cenos7 ~]# ifconfig ens33|head -n2|tail -n1 |tr -s " " |cut -d " " -f3
cenos6
[root@cenos7 ~]# ifconfig ens33|head -n2|tail -n1 |tr -s " " |cut -d " " -f4
取ip地址
nmap -v -sP 192.168.32.0/24 |grep -B1 up |grep "Nmap scan"
ifconfig eth0 |grep -w "inet" |grep -o "[0-9]{7,}"|head -n1
ifconfig |grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
ifconfig eth0 |grep -w "inet" |grep -o "[0-9.]{7,15}" |head -n1
ifconfig eth0 |grep -o "inet [0-9.]+"|cut -d " " -f2
ifconfig eth0|grep -w inet |grep -o '[0?9]{1,3}.[0?9]{1,3}.{3}[0-9]{1,3}'|head -n1
ifconfig eth0|grep -w inet |grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}'|head -n1
取出版本號(hào)
[root@cenos7 ~]# cat /etc/redhat-release |tr -dc '[:digit:].'
7.5.1804[root@cenos7 ~]#
橫向合并
paste f1 f2
縱向合并
cat fi f2
排序
sort -t: -k3 -n /etc/passwd 按數(shù)字排序
sort -t -k1 字母排序
取出現(xiàn)次數(shù)最多的行
cat f1 |sort |uniq -c
取兩個(gè)文件的交集
cat f1 f2 | sort |uniq -d
取出現(xiàn)次數(shù)最多的行
取訪問最多的IP
cut -d " " -f1 /var/log/httpd/access_log |sort |uniq -c |sort -nr |head -n1
............................................................................................................
1、找出ifconfig “網(wǎng)卡名” 命令結(jié)果中本機(jī)的IPv4地址
[root@cenos7 ~]# ifconfig ens33 cenos7
[root@cenos7 ~]# ifconfig ens33|head -n2|tail -n1 |tr -s " " |cut -d " " -f3
cenos6
[root@cenos7 ~]# ifconfig ens33|head -n2|tail -n1 |tr -s " " |cut -d " " -f4
2、查出分區(qū)空間使用率的百分比值
[root@cenos7 ~]# df | tr -s ' ' '%' | sort -nr -t'%' -k5 | cut -d'%' -f5 |head -n1
df |grep "^/dev/sd"|tr -s " " % |cut -d % -f5 |sort -nr
df|grep "/dev/sd" |grep -Eo "[0-9]{1,3}%"|grep -Eo "[0-9]{1,3}" |sort -nr
3、查出用戶UID最大值的用戶名、UID及shell類型
[root@cenos7 ~]# cat /etc/passwd |cut -d ':' -f1,3,7|sort -nr -t':' -k2 |head -n1
nfsnobody:65534:/sbin/nologin
4、查出/tmp的權(quán)限,以數(shù)字方式顯示
[root@cenos7 ~]# stat /tmp/ | head -n4 |tail -n1 | cut -d'/' -f1 | tr -dc [[:digit:]]
1777
5、統(tǒng)計(jì)當(dāng)前連接本機(jī)的每個(gè)遠(yuǎn)程主機(jī)IP的連接數(shù),并按從大到小排序
netstat -nt |tr -s ' ' ':' | cut -d : -f6 |sort |uniq -c |sort -nr
...................................................................................................................
4位數(shù)的uid號(hào)
grep -w [[:digit:]] [[:digit:]][[:digit:]] [[:digit:]] /etc/passwd
統(tǒng)計(jì)/etc/*.conf 文件個(gè)數(shù)
ls -R /etc/ |grep ".conf$" |wc -l
取出、/misc/cd/Packages/ 目錄下 以rpm 結(jié)尾的前一個(gè)詞
ls -R /misc/cd/Packages/ |grep -o ".[^.]+.rpm$" | sort | uniq -c
ls -R /misc/cd/Packages/*.rpm |rev /cut -d. -f2 |rev | sort | uniq -c
過濾空行和空白行
空行
grep -v '^$' f1
空白行
grep -v '^[[:space:]]*$' f1
.........................................................................................
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
[root@cenos7 ~]# ls /proc/meminfo |grep -i "^s"
[root@cenos7 ~]# ls /proc/meminfo |grep "^[sS]"
?2、顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行
[root@cenos7 ~]# cat /etc/passwd |grep -v "/bin/bash"
?3、顯示用戶rpc默認(rèn)的shell程序
[root@cenos7 ~]# cat /etc/passwd |cut -d: -f1,7|grep -w rpc
?4、找出/etc/passwd中的兩位或三位數(shù)
[root@cenos7 ~]# cat /etc/passwd |grep -o "[0?9][0?9]{2,3}"
?5、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個(gè)空白字符開頭的且后面有非空白字符的行
[root@cenos7 ~]# cat /etc/grub2.cfg |grep "^[[:space:]][[:space:]][[:space:]][[:space:]]"
?6、找出“netstat -tan”命令結(jié)果中以LISTEN后跟任意多個(gè)空白字符結(jié)尾的行
[root@cenos7 ~]# netstat -tan |grep "LISTEN[[:space:]].?LISTEN[[:space:]].?"
?7、顯示CentOS7上所有系統(tǒng)用戶的用戶名和UID
[root@cenos7 ~]# cat /etc/passwd |cut -d: -f1,3 | grep -w "[1?9]∥[1?9][0?9]∥[1?9][0?9][0?9]∥1000[1?9]‖[1?9][0?9]‖[1?9][0?9][0?9]‖1000"
[root@cenos7 ~]# cat /etc/passwd |cut -d: -f1,3 | grep -w "[1-9][0-9]{0,2}"
?8、添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名和shell同名的行
[root@cenos7 ~]# cat /etc/passwd |egrep -w "^(bash|testbash|basher|sh|nologin)"
?9、利用df和grep,取出磁盤各分區(qū)利用率,并從大到小排序
df |grep "^/dev/sd"|tr -s " " % |cut -d % -f5 |sort -nr
1、顯示三個(gè)用戶root、mage、wang的UID和默認(rèn)shell
1 grep -E "^(root|mage|wang)>" /etc/passwd|cut -d: -f3,7
?2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個(gè)小括號(hào)的行
[root@cenos7 ~]# egrep "^.\b[[:space:]]" /etc/rc.d/init.d/functions
?3、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo /etc/rc.d/init.d/functions|egrep -o '[^/]+$'
echo /etc/rc.d/init.d/functions|egrep -o '[^/]+/?$'
?4、使用egrep取出上面路徑的目錄名
echo /etc/rc.d/init.d/functions|egrep -o "^.*/"
?5、統(tǒng)計(jì)last命令中以root登錄的每個(gè)主機(jī)IP地址登錄次數(shù)
[root@cenos7 ~]# last |tr -s " " |sort | cut -d" " -f3 |uniq -c
?6、利用擴(kuò)展正則表達(dá)式分別表示0-9、10-99、100-199、200-249、250-255
grep -w "[0-9]"
[root@cenos7 ~]# cat aa.txt |grep -w "[1][0-9][0-9]"
[root@cenos7 ~]# cat aa.txt |grep -w "[2][0-4][0-9]"
[root@cenos7 ~]# cat aa.txt |grep -w "[2][5][0-5]"
?7、顯示ifconfig命令結(jié)果中所有IPv4地址
?8、將此字符串:welcome to magedu linux 中的每個(gè)字符去重并排序,重復(fù)次數(shù)多的排到前面