linux基础命令练习7.13

1.已知sort.log文件内容如下,请根据文件内容的第二列进行倒序排序。
cat >>sort.log<<'EOF'
218.65.30.25 68652
218.65.30.53 34326
218.87.109.154 21201
112.85.42.103 18065
112.85.42.99 17164
218.87.109.151 17163
218.87.109.150 17163
218.65.30.61 17163
218.65.30.126 17163
218.65.30.124 17163
EOF
sort  -t '.' -rnk2 sort.log
2.统计系统文件/etc/services有多少行。
grep -n '.*' /etc/services
wc -l /etc/services
3.已知文件内容如下,请对该文件的字符串出现的次数进行统计,并从小到大的进行排序出来。
cat>>uniq.txt<<'EOF'
oldboy
oldgirl
oldboy
egon
student
oldgirl
oldboy
EOF
sort uniq.txt | uniq -c | sort -n
4.取出系统中的文件/etc/passwd的第七列(以:为分隔符)。
cut -d ':' -f7 /etc/passwd
5.已知文件test.txt内容如下,请给出输出test.txt文件内容时,不包含oldboy字符串的命令。
test
qiudao
oldboy
grep -v 'oldboy' test.txt
sed '/oldboy/d'  test.txt
awk  '!/oldboy'   test.txt

6.只查看/etc/passwd文件内第5到第15行的内容
head -15 /etc/passwd | tail -11
grep -n '.*' /etc/passwd | grep -wA  10  '^5'
sed -n '1,15p'  /etc/passwd
awk 'NR>4 && NR<16' /etc/passwd
awk 'NR==5,NR==15' /etc/passwd
 
7.test.txt 的内容如下,要求过滤出不包含oldgirl的行。
oldboy
oldgirl
qiudao
grep -v 'oldgirl'  test.txt
sed  '/oldgirl/d'  test.txt
awk  '!/oldgirl/'  test.txt

8.请执行命令取出linux中eth0的IP地址分别是ip和ifconfig的命令(请用cut,有能力者也可分别用awk,sed命令答)。
ip a s eth0 | head -3 | tail -1 | tr '/' ' | ' cut -d ' ' -f6
ip a s eth0| sed -nr '3s#(.*t )(.*)(/.*)#2#gp'
ip a s eth0|awk -F '[ /]' 'NR==3{print $6}'
ifconfig eth0|sed -nr '2s#(.*et )(.*)(n.*)#2#gp'
ifconfig eth0|awk 'NR==2{print $2}'
9.删除/etc/passwd文件的前三行内容。
grep -n '.*' /etc/passwd |  grep  -wv   '^[123]'
grep -n '.*' /etc/passwd |  grep  -wEv   '^1|^2|^3'
sed  '1,3d' /etc/passwd
awk 'NR>3'  /etc/passwd
10.统计文件/etc/hosts的行数?你能使用几种方法实现?
wc -l  /etc/hosts 
grep -c '.*'  /etc/hosts
11.统计文件/etc/services的字节数。你能使用几种方法实现?
ll  /etc/services
wc -c /etc/services
12.执行下面的命令echo "Im qls , is QQ 1176494252" >file.txt,要求取出该文件中的姓名和QQ号。
cut -d ' ' -f2,6 file.txt
awk  ‘{print $2;$6}’ filr.txt
13.执行如下命令
cat > file.txt <<EOF
abc
123
abc
123
def
EOF
要求去除重复的列
sort  file.txt | uniq
14.接上题,取出每行出现的次数
sort  file.txt | uniq -c
15.统计/var/log下的文件个数,不统计下级目录。
tree -L 1 /var/log |tail -1|awk '{print $(NF-1)}'
ll /var/log|grep -c '^-'
ll /var/log|grep  '^-'|wc -l
16.统计/var/log下的所有文件和目录个数,不包含隐藏文件。
tree  /var/log |tail -1|awk '{print $1,$(NF-1)}'
tree  /var/log |tail -1|cut -d ' ' -f1,3
17.以“:”为分隔符,取出/etc/passwd第一行的最后一列的内容
head -1 /etc/passwd  | cut -d ':' -f7
awk -F ':' 'NR==1{print $(NF)}' passwd
sed -nr '1s#(.*:)(.*)#2#gp' passwd

18.过滤出/etc/passwd以nologin结尾的内容,并统计行数
 grep   -c   'nologin$'     /etc/passwd
19.分析如下日志内容,每个域名被访问次数
cat>web.log <<EOF
http://www.oldboy.com/index.html
http://www.oldboy.com/1.html
http://post.oldboy.com/index.html
http://mp3.oldboy.com/index.html
http://www.oldboy.com/3.html
http://post.oldboy.com/2.html
EOF
sed -r 's#(.*//)(.*)(/.*)#2#g'  web.log |  sort  | uniq -c
awk -F '[///]' '{print $3}' web.log |sort |uniq -c
cut -d '/' -f3 web.log |sort  | uniq -c
20.显示/etc/services文件的第11行到第20行的内容
head -20 /etc/services | tail -10
grep -n '.*' /etc/services | grep -wA  9 '^11'
less -10 +11 /etc/services
sed -n '11,20p' /etc/services
awk 'NR==11,NR==20'   /etc/services
awk 'NR>10 && NR<21' /etc/services
21.已知文件123.txt内容如下,请过滤出包含oldboy字符串的命令
test
OLDBOY
online
oldboy
oldboyoldboy
grep 'oldboy'  123.txt
sed  -n '/oldboy/p' 123.txt
awk '/oldboy/' 123.txt
22.过滤出文件123.txt中不包含test的行,并给其过滤出来的内容加上行号
grep   -nv  'test'  123.txt
awk '!/test/{print NR,$0}' 123.txt
 
23.要求过滤出文件123.txt中包含oldboy的字符串,忽略大小写。
grep -i 'oldboy'  123.txt
sed  -nr '/oldboy/pi'  123.txt
awk   '/oldboy|OLDBOY/'   123.txt 
24.要求过滤出文件123.txt中包含online字符串的行,并统计共有多少行。
grep  -c  'online' 123.txt
 

25.要求过滤出文件123.txt中oldboy的单词。
grep -w 'oldboy'  123.txt
写一个文件,文件内容如下,下面几题请用该文件作答?
cat >>test.txt<<EOF
server {
 lisTEN 80;
 server_nAme www.oldboy.com;
 root /code/dOcs
 index INDEX.html;
}
EOF
26.过滤www.oldboy.com这段关键字
grep 'www.oldboy.com'  test.txt
sed  -n  '/www.oldboy.com/p' test.txt
awk  '/www.oldboy.com/'   test.txt
 
27.同时过滤出root和index的行,不区分大小写
grep  -iE 'root|index' test.txt 
28.过滤index,区分大小写
grep 'index' test.txt
sed -n '/index/p'   test.txt
awk  '/index/'  test.txt

29.过滤出带"O"的行,不区分大小写
grep -i 'O'  test.txt
sed  -nr '/o|O/p' test.txt 
awk  '/o|O/' test.txt

30.过滤出不带";"的行
grep -v  ';'  test.txt
sed  '/;/d'  test.txt
awk  '!/;/'   test.txt

31.过滤出以s开头的行
grep '^s'  test.txt
sed  -n  '/^s/p'  test.txt
awk  '/^s/'  test.txt
 
32.统计该文件的行数
grep -c '.*'  test.txt
wc -l  test.txt
33.如果某一天你误操作了"rm -rf *",会发生哪些情况
删除当前目录下的所有
34.已知123.txt文件内容如下:
linlaoshi
qiulaoshi
oldboy
要求过滤出oldboy这一行的内容?你有几种方法?
grep 'oldboy'  123.txt
sed -n '/oldboy/p' 123.txt
awk '/oldboy/'  123.txt

35.接上题,要求不显示oldboy这行内容,怎么实现?你有几种方法?
 grep -v 'oldboy'  123.txt
sed  '/oldboy/d' 123.txt
awk '!/oldboy/'  123.txt
 
 
 
 
 
原文地址:https://www.cnblogs.com/chenlifan/p/13331180.html