Shell 脚本(五) Shell 工具 及 企业面试题

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

十、Shell工具(重点)

1、cut

cut 的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。 cut 命令从文件的每一好难过剪切字节、字符和字段并将这些字节、字符和字段输出。

1.1 基本用法

 cut [选项参数] filename

 说明: 默认分隔符是制表符

1.2 选项参数说明

   选项参数   功能
   -f 列好,提取第几列
  -d

分隔符,按照指定分隔符分割列

 1.3 案例实操

(0)数据准备

[root@rich datas]# touch cut.txt
[root@rich datas]# vim cut.txt

dong shen   //一个空格
guan zhen   //一个空格
wo  wo      //两个空格,后者属于第三列
lai  lai    //两个空格,后者属于第三列
le  le      //两个空格,后者属于第三列

(1)切割 cut.txt 第一列

[root@rich datas]# cut -d " " -f 1 cut.txt 
dong
guan
wo
lai
le

(2)切割 cut.txt 第二、三列

[root@rich datas]# cut -d " " -f 2,3 cut.txt 
shen
zhen
 wo
 lai
 le

(3)在cut.txt文件中切割出 guan

[root@rich datas]# cat cut.txt |grep guan|cut -d " " -f 1
guan

(4)选取系统PATH变量值,第2个“:”开始后的所有路径:

[root@rich datas]# echo $PATH
/opt/jdk1.8.0_121/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@rich datas]# echo $PATH|cut -d : -f 3-
/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

(5)切割 ifconfig 后打印的IP地址

[root@rich datas]# cat cut.txt|grep inet 
inet 192.168.254.118  netmask 255.255.255.0
[root@rich datas]# ifconfig ens33 | grep "inet "|cut -d "t" -f 2|cut -d " " -f 2
192.168.254.118

2、sed

sed 是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,知道文件末尾。文件内容并没有改变,除非你使用重定向存储输出。 

2.1基本用法

  sed[选项参数]  ‘command’ filename

2.2 选项参数说明

选项参数  功能
  -e 直接在指令列模式上进行sed 的动作编辑

2.3 命令功能描述

命令

 功能描述

 a

新增, a 的后面可以接字符串,在下一行出现
 d 删除
 s 查找并替换

2.4 案例实操

(0)数据准备

[root@rich datas]# vim sed.txt 
[root@rich datas]# cat sed.txt 
dong shen
guan zhen
wo  wo 
lai  lai 

le  le

(1)将“mei nv” 这个单词插入到 sed.txt 第二行下,打印

[root@rich datas]# sed "2a mei nv" sed.txt 
dong shen
guan zhen
mei nv
wo  wo 
lai  lai 

le  le

注意: 文件并没有改变

(2)删除sed.txt 文件所有包含 wo 的行

[root@rich datas]# sed "/wo/d" sed.txt
dong shen
guan zhen
lai  lai 

le  le

(3)将sed.txt 文件中 wo 替换为 ni

[root@rich datas]# sed "s/wo/ni/g" sed.txt
dong shen
guan zhen
ni  ni 
lai  lai 

le  le

注意: ‘g’表示 global,全部替换

(4)将sed.txt 文件中的第二行删除并将 wo 替换为 ni

[root@rich datas]# sed -e "2d" -e "s/wo/ni/g" sed.txt
dong shen
ni  ni 
lai  lai 

le  le

3、 awk

一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理

3.1 基本用法

 awk[选项参数]   ‘patternl{action1} patternl2{action2}....’ filename

pattern: 表示 AWK 在数据中查找的内容,就是匹配模式

action :  在找到匹配内容是所执行的一系列命令

3.2 选项参数说明

选项参数 功能
   -F 指定输入文件拆分隔符
   -v 赋值一个用户定义变量

3.3 案例实操

 (0)数据准备

cp /etc/passwd ./

[root@rich datas]# chown root:root passwd 

[root@rich datas]# cat passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

 (1)搜索passwd 文件以 root 关键字开头的所有行,并输出改行的第七列。

[root@rich datas]# awk -F : '/^root/ {print $7}' passwd 
/bin/bash

 (2)搜索 passwd 文件以 root 关键字开头的所有行,并输出改行的第一列和第七列,中间以 “ ,” 好分割

[root@rich datas]# awk -F : '/^root/ {print $1","$7}' passwd 
root,/bin/bash

注意: 只有匹配了pattern  的行才会执行action

  (3) 只显示/etc/passwd 的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell 在最后一行添加“dahaige, /bin/zuishuai”。

[root@rich datas]# awk -F : 'BEGIN{print "user,shell"} {print $1","$7} END{print "wenmin,bin/zuimei"}' passwd 
user,shell
root,/bin/bash
bin,/sbin/nologin
.................
mysql,/bin/bash
elasticsearch,/sbin/nologin
wenmin,bin/zuimei

注意: BEGIN 在所有数据读取行之前执行;END 在所欲数据执行之后执行。

(4) 将passwd 文件中的用户 id  增加数值 1 并输出

[root@rich datas]# awk -F : -v i=1 '{print $3+i}' passwd 

1
2
3
4
5
6
7
8
9

3.4 awk 的内置变量

变量 说明
FILENAME 文件名
NR 已读的记录数
NF 浏览记录的域的个数(切割后,列的个数)

 3.5 案例实操

(1)统计passwd 文件名,每行的行号,每行的列数

[root@rich datas]# awk -F : '{print FILENAME ","NR"," NF}' passwd 
passwd,1,7
passwd,2,7
passwd,3,7
。。。。。。。。。。。。

(2)切割IP

[root@rich datas]# ifconfig ens33 | grep "inet " |awk -F " " '{print $2}'
192.168.254.118

(3)查询 sedtxt 中空行所在的行号

[root@rich datas]# awk '/^$/{print NR}' sed.txt 
5
[root@rich datas]# cat sed.txt 
dong shen
guan zhen
wo  wo 
lai  lai 

le  le

4、 sort

  sort  命令是在 Linux 里非常有用,它将文件进行排序,并将排序结果标准输出。

4.1 基本语法

 sort(选项参数)

选项 说明
 -n 依照数值的大小排序
 - r 以相反的顺序来排序
 -t 设置排序时所用的分隔字符
 -k 指定需要排序的列

参数: 指定待排序的文件列表

4.2 案例实操

(0)数据准备

[root@rich datas]# touch sort.sh
[root@rich datas]# vim sort.sh 
[root@rich datas]# cat sort.sh 
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6

(1)按照 “ :” 分割后的第三列倒序排序

[root@rich datas]# sort -t : -nrk 2 sort.sh 
xz:50:2.3
bb:40:5.4
ss:30:1.6
bd:20:4.2
cls:10:3.5

十一、企业真实面试题

1、京东

 问题1: 使用Linux 命令查询file1中空行所在的行号

答案:

[root@rich datas]# awk '/^$/{print NR}' sed.txt 
5

问题2: 有文件 chengji.txt 内容如下:

     张三 40

     李四 50

     王五 60

使用Linux 命令计算第二列的和并输出

[root@rich datas]# cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
150

2、搜狐&和讯网

问题1: Shell 脚本里如何检查一个文件是否存在? 如果不存在该如何处理?

#!/bin/bash

if [ -f file.txt ]; then
	echo "文件存在!"
else
	echo "文件不存在"
fi

3、新浪

问题1: 用Shell写一个脚本,对文本中无序的一列数字排序

root@rich datas]# touch test.txt
[root@rich datas]# vim test.txt 
[root@rich datas]# sort -n test.txt | awk '{a+=$0;print $0}END{print "SUM="a}'
1
2
2
3
4
5
6
7
8
9
10
SUM=57

4、金和网络

问题1: 请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符“shen”的文件名称

[root@rich datas]# grep -r shen /home
/home/wenmin/datas/cut.txt:dong shen
/home/wenmin/datas/sed.txt:dong shen
[root@rich datas]# grep -r shen /home | cut -d " " -f 1
/home/wenmin/datas/sed.txt:dong
[root@rich datas]# grep -r shen /home | cut -d " " -f 2
shen
shen
原文地址:https://www.cnblogs.com/wushaopei/p/11979263.html