2018-08-07文本处理练习

取第二行
[root@cenos7 ~]# ifconfig ens33

ens33: flags=4163 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

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

取随机数 只要数字加字母 取前10个
cat /dev/urandom

openssl rand 产生随机数

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

取出版本号
[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 按数字排序

sort -t -k1 字母排序

取出现次数最多的行

cat f1 |sort |uniq -c

取两个文件的交集

cat f1 f2 | sort |uniq -d

取出现次数最多的行
取访问最多的IP
cut -d " " -f1 /var/log/httpd/access_log |sort |uniq -c |sort -nr |head -n1

............................................................................................................

1、找出ifconfig “网卡名” 命令结果中本机的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、查出分区空间使用率的百分比值
[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的权限,以数字方式显示
[root@cenos7 ~]# stat /tmp/ | head -n4 |tail -n1 | cut -d'/' -f1 | tr -dc [[:digit:]]

1777

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
netstat -nt |tr -s ' ' ':' | cut -d : -f6 |sort |uniq -c |sort -nr

...................................................................................................................

4位数的uid号
grep -w [[:digit:]] [[:digit:]][[:digit:]] [[:digit:]] /etc/passwd

统计/etc/*.conf 文件个数
ls -R /etc/ |grep ".conf$" |wc -l

取出、/misc/cd/Packages/ 目录下 以rpm 结尾的前一个词
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结尾的行
[root@cenos7 ~]# cat /etc/passwd |grep -v "/bin/bash"

3、显示用户rpc默认的shell程序
[root@cenos7 ~]# cat /etc/passwd |cut -d: -f1,7|grep -w rpc

4、找出/etc/passwd中的两位或三位数

[root@cenos7 ~]# cat /etc/passwd |grep -o "[0−9][0−9]{2,3}"

5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
[root@cenos7 ~]# cat /etc/grub2.cfg |grep "^[[:space:]][[:space:]][[:space:]][[:space:]]"

6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行
[root@cenos7 ~]# netstat -tan |grep "LISTEN[[:space:]].∗LISTEN[[:space:]].∗"

7、显示CentOS7上所有系统用户的用户名和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,取出磁盘各分区利用率,并从大到小排序
df |grep "^/dev/sd"|tr -s " " % |cut -d % -f5 |sort -nr

1、显示三个用户root、mage、wang的UID和默认shell

1 grep -E "^(root|mage|wang)>" /etc/passwd|cut -d: -f3,7

2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[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、统计last命令中以root登录的每个主机IP地址登录次数

[root@cenos7 ~]# last |tr -s " " |sort | cut -d" " -f3 |uniq -c

6、利用扩展正则表达式分别表示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命令结果中所有IPv4地址

8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

你可能感兴趣的:(2018-08-07文本处理练习)