image

编辑人: 长安花落尽

calendar2025-06-05

message8

visits778

Linux Shell笔试/面试题

查询file里面空行的所在行号

$ awk '{if($0~/^$/)print NR}' file
3
5

$ grep -n ^$ file |awk 'BEGIN{FS=":"}{print $1}'
3
5

$ grep -n ^$ file |tr -d ":"
3
5

查询file以33结尾的行

$ grep "33$" file
31 32 33

打印出file文件第1到第3行

$ sed -n '1,3p' file
11 12 13
21 22 23

$ head -3 file
11 12 13
21 22 23

统计排序

域名数量统计及排序
http://a.domain.com/1.html
http://b.domain.com/1.html
http://c.domain.com/1.html
http://a.domain.com/2.html
http://b.domain.com/2.html
http://a.domain.com/3.html

[root@HADOOP-215 ~]# awk -F "/" '{print $3}' domain.txt |sort |uniq -c |sort -nr
      3 a.domain.com
      2 b.domain.com
      1 c.domain.com

[root@HADOOP-215 ~]# awk 'BEGIN{FS="/"}{arr[$3]++}END{for(i in arr)print arr[i],i}' domain.txt |sort -r
3 a.domain.com
2 b.domain.com
1 c.domain.com
Nginx每秒访问数
[root@HADOOP-215 wwwlogs]# awk '{print $4}' access.log |sed -r 's/.*[0-9]{4}://g' |sort |uniq -c |sort -nr |head -10
    132 02:43:53
    123 02:50:48
    122 14:30:34
    121 00:52:58
    118 23:34:23
    109 15:20:21
    108 16:28:35
    108 04:50:26
    106 20:04:50
    106 00:20:53

随机数及字符串翻转

如果得到随机的字串,长度和字串中出现的字符表可定义,并将字串倒序显示,如把0123456789 作为基准的字串字符表,产生一个6 位的字串642031,打印出的字串为130246

[root@HADOOP-215 ~]# str="";for i in `seq 1 6`;do let a=$RANDOM%9; str=$str$a;done ;echo $str ;echo $str |rev
223885
588322

[root@HADOOP-215 ~]# awk -v count=6 'BEGIN{srand();str="0123456789";len=length(str);for(i=count;i>0;i--)marry[i]=substr(str,int(rand()*len),1);for(i=count;i>0;i--)printf("%c",marry[i]);printf("n");for(i=0;i<=count;i++)printf("%c",marry[i]);printf("n")}'
260584
485062

/proc/sys/ 目录

该子目录的作用是报告各种不同的内核参数,并让您能交互地更改其中的某些。与 /proc中所有其他文件不同,该目录中的某些文件可以写入,不过这仅针对 root。其中的目录以及文件的详细列表将占据过多的篇幅,而且该目录的内容是依赖于系统的,而大部分的文件也仅仅对某些特殊的应用程序有用。然而,以下是该子目录的两个最常见的用途:

  • 允许路由:即便是 Mandrakelinux 默认的内核也是允许路由的,您必需显式允许它这么做。为此,您只要以 root 身份键入以下命令:

    $ echo 1 >/proc/sys/net/ipv4/ip_forward

  • 阻止 IP 欺骗:IP 欺骗会让人认为某个来自于外部的某个数据包是来自于它到达的那个接口。这一技术常被骇客(cracker)所使用。您可以让内核阻止这种入侵。请键入:

    $ echo 1 >/proc/sys/net/ipv4/conf/all/rp_filter

这些改变仅当系统运行时有效。在系统重新启动之后,它们会改会它们的默认值。要在启动时就改动这些值,您可以将您在 shell 提示符后键入的命令添加到 /etc/rc.d/rc.local 中以免每次都键入它们。另一个方法是修改 /etc/sysctl.conf

文件指定行合并

奇偶行合并

[root@HADOOP-215 tmp]# cat 1.txt 
1   12
2   3d
3   123
4   98
56  76
[root@HADOOP-215 tmp]# sed '$!N;s/n/ /g' 1.txt 
1   12 2   3d
3   123 4   98
56  76

指定行合并

[root@HADOOP-215 tmp]# sed -n -e 2p -e 3p 1.txt |sed '$!N;s/n/ /g'
2   3d 3   123

如果不是文件的最后一行,就往后读一行,$是最后一行,!是不配匹 N读入下一行

流操作

< file means open a file for reading and associate with STDIN. 
<< token Means use the current input stream as STDIN for the program until token is seen. We will ignore this one until we get to scripting. 
> file means open a file for writing and truncate it and associate it with STDOUT. 
>> file means open a file for writing and seek to the end and associate it with STDOUT. This is how you append to a file using a redirect. 
n>&m means redirect FD n to the same places as FD m. Eg, 2>&1 means send STDERR to the same place that STDOUT is going to. 

#EOF之间的内容追加至文件file
cat >>file<<EOF
some text
EOF


喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:Linux Shell笔试/面试题

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share