当前位置:   article > 正文

shell脚本编程实战_shell脚本编程实战手册

shell脚本编程实战手册

shell基础语法
1 变量替换

语法说明
${变量名#匹配规则}从变量开头进行匹配,将符合最短的数据删除
${变量名##匹配规则}从变量开头进行匹配,将符合最长的数据删除
${变量名%匹配规则}从变量尾部进行规则匹配,将符合最短的数据删除
${变量名%%匹配规则}从变量尾部进行规则匹配,将符合最长的数据删除
${变量名/旧字符串/新字符串}变量内容符合旧字符串,则第一个旧字符串会被新字符串取代
${变量名//旧字符串/新字符串}变量内容符合旧字符串,则全部的旧字符串会被新字符串取代
#代码实践
var1="May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? "
var2=${var1#*enough} #表示删除从左侧匹配到的第一个*enough及其左侧的内容,*表示任意多个字符
var3=${var1##*enough} #表示删除从左侧匹配到的最后一个*enough及其左侧的内容,*表示任意多个字符
var4=${var1%enough*} #表示删除从右侧匹配到的第一个*enough及其右侧的内容,*表示任意多个字符
var5=${var1%%enough*} #表示删除从右侧匹配到的最后一个*enough及其右侧的内容,*表示任意多个字符
var6=${var1/enough/ENOUGH} # 变量替换,将从左侧匹配到的第一个enough替换为ENOUGH 
var7=${var1//enough/ENOUGH} # 变量替换,将从左侧匹配到的全部enough替换为ENOUGH

echo $var2 
====>
happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy?

echo $var3
===>
hope to make you happy?

echo $var4
===>
May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,

echo $var5
===>
May you have

echo $var6
===>
May you have ENOUGH happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy?


echo $var7
===>
May you have ENOUGH happiness to make you sweet,ENOUGH trials to make you strong,ENOUGH sorrow to keep you human,ENOUGH hope to make you happy?

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

字符串处理
1 计算字符串长度

一、 ${#str}  
str="hello"
echo ${#str}
===>
5

二、 expr length "$str"
str="hello world"
echo `expr length "$str"`
===>
11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2 获取字串中某个字符在字符串中的索引位置

expr index $str $substr   # 当字符串中带空格时需要使用引号包裹,否则会报错,使用expr使,索引下标是从1开始的

str="hello world" 
echo `expr index "$str" or` 
===>
5

echo `expr index "$str" a`
===>
0

echo `expr index "$str" th`
====>
1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3 计算从头匹配上的子串长度

expr match $str substr  

str="this is a book"
echo `expr match "$str" book`
===>
0  #没匹配上

echo `expr match "$str" *.is`  
====>
0

echo `expr match "$str" this`
===>
4

echo `expr match "$str" this.*`
===>
14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4 抽取字串

1 ${str:position}  从str中的position开始到最后,索引从0开始
str="hadoop java scala mapreduce spark"
echo ${str:10}
===>
a scala mapreduce spark

2 ${str:position:length} 从position开始,截取长度为length,索引从0开始
str="hadoop java scala mapreduce spark"
echo ${str:7:4}
===>
java

3 ${str: -position} 从右边开始的position位置开始截取,索引从0开始,字符串和position之间需要有一个空格,右边第一个字符的索引是-1
str="hadoop java scala mapreduce spark"
echo ${str: -5}
===>
spark

4 ${str:(position)} 从右边开始的position位置开始截取,索引从0开始,该方法与3同样的效果
str="hadoop java scala mapreduce spark"
echo ${str:(-5)}
===>
spark

5 expr substr $str position length  从position开始,匹配长度为length,使用expr,字符串索引从1开始
str="hadoop java scala mapreduce spark"
echo `expr substr "$str" 13 5`
===>
scala
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

5 需求描述

变量str="Bigdata process framework is Hadoop.Hadoop is an open source project"
执行脚本后,打印输出str字符串变量,并给出用户以下选项:
1 打印str长度
2 删除字符串中所有的Hadoop
3 替换第一个Hadoop为Mapreduce
4 替换全部Hadoop为Mapreduce
用户输入数字1,2,3,4,可以执行对应项的功能,输入q|Q则退出交互模式
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

代码实践

脚本名  str_test.sh
#! /bin/bash

str="Bigdata process framework is Hadoop.Hadoop is an open source project"
echo "原字符串为  ${str}"

case $1 in
         1)
         echo "${#str}";;
         2)
         str=${str//Hadoop/};;
         3)
         str=${str/Hadoop/Mapreduce};;
         4)
         str=${str//Hadoop/Mapreduce};;
         q)
         exit 0;;
         Q)
         exit 0;;
esac

echo "处理后的字符串为: ${str}"

str_test.sh 1
===>
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project
68
处理后的字符串为: Bigdata process framework is Hadoop.Hadoop is an open source project


str_test.sh 2
===>
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project
处理后的字符串为: Bigdata process framework is . is an open source project

str_test.sh 3
===>
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project
处理后的字符串为: Bigdata process framework is Mapreduce.Hadoop is an open source project

str_test.sh 4
===>
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project
处理后的字符串为: Bigdata process framework is Mapreduce.Mapreduce is an open source project

str_test.sh q
===> 没有执行后面的代码
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project

str_test.sh Q
===> 没有执行后面的代码 说明退出了
原字符串为  Bigdata process framework is Hadoop.Hadoop is an open source project
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

优化后的代码

#! /bin/bash

# 打印程序提示信息
function print_tips(){
    echo "******************************************"
	echo "1) 打印str长度"
	echo "2) 删除字符串中所有的Hadoop"
	echo "3) 替换第一个Hadoop为Mapreduce"
	echo "4) 替换全部Hadoop为Mapreduce"
	echo "q|Q 退出程序"
	echo "*******************************************"
}

# 返回传入参数的字符串长度
function getStrLength(){
     return ${#1}
}

# 删除字符串中所有的某个值,第一个参数表示从删除前的原字符串,第二个参数表示删除的字符串,记得
function delete_str_all(){
     str="${1//$2/}"
}

function replace_str(){
    str="${1/$2/$3}"
}

function replace_str_all(){
   str="${1//$2/$3}"
}


while true 
do
     str="Bigdata process framework is Hadoop.Hadoop is an open source project"
     print_tips
     read -p "please input one of these '1|2|3|4|q|Q'" varible
	 case $varible in 
	         1)
			 getStrLength "$str"
			 echo $?
			 echo "$str"
			 ;;
			 2)
			 delete_str_all "$str" Hadoop
			 echo "$str"
			 ;;
			 3)
			 replace_str "$str" Hadoop Mapreduce
			 echo "$str"
			 ;;
			 4)
			 replace_str_all "$str" Hadoop Mapreduce
			 echo "$str"
			 ;;
			 q|Q)
			 exit 0
			 ;;
			 *)
			 echo "Error,pleast iput one of these '1|2|3|4|q|Q'"
			 ;;
	 esac		 
done
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

命令替换(引用命令的返回结果)

1 `command`
2 $(command)

1 获取所有的系统用户并输出
cat /etc/passwd | cut -d ":" -f 1

index=1
for user in `cat /etc/passwd | cut -d ":" -f 1`
do
    echo "user_${index} is : ${user}"
    index=$((index + 1))
done 

2 根据系统实践计算当年和明年
echo "今年是$(date +%Y)年"
echo "今年是`date +%Y`年"
echo "明年是$(($(date +%Y) + 1))年"
echo "明年是$((`date +%Y` +1))年"

3 判断某个进程是否存在,如果不存在就启动
mysql_process_num=$(ps -ef | grep mysqld | grep -v grep | wc -l)
if [ $mysql_process_num -eq 0 ];then  
     service mysqld start
fi
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

声明有类型变量

1 declare 命令和typeset命令两者等价
2 以上两个关键字是用来定义变量类型的
  • 1
  • 2
参数含义
-r将变量设为只读
-i将变量设为整数
-a将变量定义为数组
-f显示此脚本前定义过的所有函数及内容
-F仅显示此脚本前定义过的 函数名
-X将变量声明为环境变量
-- 1 声明只读变量
declare -r str="hello" 
-- 修改变量的值会报错
str="aa"
===>
-bash: str: readonly variable

-- 2 将一个变量声明为整型
不特殊声明的情况下
num1=10
num2=$num1+20
echo $num2
===>  该表达式会被当做字符串拼接处理
10+10

num1=10
declare -i num2
num2=$num1+20
echo $num2
===>
20

3  查看函数
在脚本中使用declare -F 可以显示该脚本之前定义的函数名称
在脚本中使用declare -f 可以显示该脚本之前定义的函数名称和内容


4 声明数组
declare -a array=("java" "scala" "spark" "hadoop" "azkaban")
# 输出全部内容
echo ${array[@]}
===>
java scala spark hadoop azkaban

# 输出第1个元素
echo ${array[0]}
===>
java

# 访问数组指定索引范围的元素
echo ${array[@]:1:4}  # 显示数组下标1到4之间的元素
===>
scala spark hadoop azkaban

# 遍历数组
for i in ${array[@]}
do
   echo $i
done
===>
java
scala
spark
hadoop
azkaban

# 为2个元素赋值
array[1]="hello" 

 # 输出数组的元素个数
echo ${#array[@]}
===>
5

# 输出数组第2个元素的长度
echo ${#array[1]} 
===>
5

# 清空数组中的某个元素
unset array[2]
echo ${array[@]}
===>
java hello hadoop azkaban

# 清除整个数组
unset array
echo ${array[@]}
===>
什么都没有


6 声明为环境变量 declare -x
在终端中num=10
进入脚本,echo $num
===>没有值输出

若将变量num声明为环境变量
declare -x num
执行上面的脚本,成功输出10,说明定义环境变量,可以在不同脚本中引用

取消声明的变量
declare +r
declare +i
declare +a
declare +x
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

shell脚本的特殊变量

$0  当前脚本的文件名
$#  传递给脚本或者函数的参数个数
$*  传递给脚本或者函数的所有参数,将所有参数作为一个整体返回,无法分割
$@  传递给脚本或者函数的所有参数,将所有参数分开返回,可以分割参数
$?  上个命令的退出状态或函数的返回值
$$  当前进程的id号,对于shell脚本就是脚本所在的进程id
$n  传递给脚本或者函数的参数,n是数字,表示第几个参数,若为10个及以上参数,那么使用${n}表示,${10}

#! /bin/bash

echo "当前脚本的文件名是:$0"
echo "传递的参数个数:$#"
echo "传入的所有参数*:$*"
echo "传入的所有参数@: $@"
echo "脚本所在的进程id: $$"
echo "上一条命令的退出状态: $?"
echo "第10个参数:${10}"

for i in "$*"
do
   echo "这里是*的所有参数: $i"
done

for i in "$@"
do
   echo "这里是@的所有参数: $i"
done

执行脚本 sh test.sh 10 20 30 40 50 60 70 80 90 100 110
===>
当前脚本的文件名是:test.sh
传递的参数个数:11
传入的所有参数*:10 20 30 40 50 60 70 80 90 100 110
传入的所有参数@: 10 20 30 40 50 60 70 80 90 100 110
脚本所在的进程id: 6083
上一条命令的退出状态: 0
第10个参数:100
这里是*的所有参数: 10 20 30 40 50 60 70 80 90 100 110
这里是@的所有参数: 10
这里是@的所有参数: 20
这里是@的所有参数: 30
这里是@的所有参数: 40
这里是@的所有参数: 50
这里是@的所有参数: 60
这里是@的所有参数: 70
这里是@的所有参数: 80
这里是@的所有参数: 90
这里是@的所有参数: 100
这里是@的所有参数: 110
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

生产中监控某种服务是否在启动状态

ps -ef | grep 服务名 | grep -v grep


  • 1
  • 2
  • 3

sed (Stream editor),流编辑器,对标准输出或文件逐行进行处理
sed工作流程
在这里插入图片描述

sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。
sed的工作流程主要包括读取、执行和显示三个过程:
①读取: sed从输入流 (文件、管道、标准输入) 中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)
②执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
③显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
注意:默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

语法格式

# 标准输出 | set 选项 "选择哪些匹配到的行进行处理(如果没有指定pattern,则是对所有行进行处理)  对选中的行处理的命令"
1 stdout | sed [option] "pattern command"

2 sed [option] "pattern command" file
  • 1
  • 2
  • 3
  • 4

sed的 常用option 选项

选项含义
-n只打印模式匹配行
-e直接在命令行进行sed编辑,默认选项
-f编辑动作保存在文件中,指定文件执行
-r支持扩展正则表达式
-i直接修改文件内容:可统一修改配置文件之类的
# 文件准备
# content.txt
Through the sea of darkness, 
hope is the light that brings us comfort, faith, and reassurance. 
It guides our way if we are lost and gives us a foothold on our fears. 
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
where hope is needed more than ever but cannot be discerned. 
Finding that is rare while the world lives in fear, 
but the belief in something better, something bigger than this,
is what keeps life worth living.

# 执行脚本
sed 'p' /usr/local/peixun/content.txt
===> 文件的每一行都打印了两遍
Through the sea of darkness,
Through the sea of darkness,
hope is the light that brings us comfort, faith, and reassurance.
hope is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
We live in a world that is disintegrating into a vicious hatred,
where hope is needed more than ever but cannot be discerned.
where hope is needed more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
Finding that is rare while the world lives in fear,
but the belief in something better, something bigger than this,
but the belief in something better, something bigger than this,
is what keeps life worth living.
is what keeps life worth living.


# -n 只打印匹配成功的行,并屏蔽自动打印模式空间的数据
sed -n '/hope/p' /usr/local/peixun/content.txt
===> 只打印匹配成功的行
hope is the light that brings us comfort, faith, and reassurance.
The moment we lose hope is the moment we surrender our will to live.
where hope is needed more than ever but cannot be discerned.

# -e 默认选项,但是如果有多个执行的命令时每一个命令前都需要加-e,否则报错
sed -n '/hope/p' '/world/p' /usr/local/peixun/content.txt
====>
sed: can't read /world/p: No such file or directory

sed -n -e '/hope/p' -e '/world/p' /usr/local/peixun/content.txt
===> 打印了包含hope或者包含world的行
hope is the light that brings us comfort, faith, and reassurance.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
where hope is needed more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,

# -f sed根据文件中配置的信息执行命令,通常有比较复杂的条件时可以使用文件配置
# peizhi.txt
/comfort/p
/cann.*/p

sed -n -f peizhi.txt content.txt
===> 打印出了符合其中任何一个条件的行
hope is the light that brings us comfort, faith, and reassurance.
where hope is needed more than ever but cannot be discerned.

# -r 支持正则扩展
cat /usr/local/peixun/content.txt | sed -n '/light|way/p' 
===>
没有任何数据返回,

cat /usr/local/peixun/content.txt | sed -n -r '/light|way/p' 
===> 使用-r后能够识别正则扩展表达式
hope is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.

linux的正则表达式分为基本正则表达式和扩展正则表达式,默认情况下sed支持基本正则表达式
加上-r选项可支持扩展正则表达式
# 筛选出含有连续两个o的行
cat /usr/local/peixun/content.txt | sed -n '/\(o\)\{2\}/p'
===>
It guides our way if we are lost and gives us a foothold on our fears.

cat /usr/local/peixun/content.txt | sed -n -r '/(o){2}/p'
===> 看起来,扩展表达式使用起来更加简便
It guides our way if we are lost and gives us a foothold on our fears.

# -i 修改文件内容选项
sed -n 's/hope/HOPE/g;p' /usr/local/peixun/content.txt
===> 该操作不影响文件本身,只有使用-i时才会修改文件本身的内容
Through the sea of darkness,
HOPE is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose HOPE is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
where HOPE is needed more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in something better, something bigger than this,
is what keeps life worth living.

sed -i 's/hope/HOPE/g' /usr/local/peixun/content.txt
cat /usr/local/peixun/content.txt
===>
Through the sea of darkness,
HOPE is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose HOPE is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
where HOPE is needed more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in something better, something bigger than this,
is what keeps life worth living.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

sed pattern

匹配模式含义
10command匹配到第10行
10,20command匹配从第10行开始,到第20行结束
10,+5command匹配从第10行开始,到第15行结束
/pattern1/command匹配到pattern1的行
/pattern1/,/pattern2/command匹配到pattern1的行开始,到匹配到pattern2的行结束
10,/pattern1/command匹配从第10行开始,到匹配到pattern1的行结束
/pattern1/,10command匹配到pattern1的行开始,到第10行匹配结束
# 指定行号
sed -n '5p' /usr/local/peixun/content.txt
===>
We live in a world that is disintegrating into a vicious hatred,

# 指定起始行和结束行
sed -n '2,4p' /usr/local/peixun/content.txt
===>
hope is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.

# 指定起始行,和后面几行
sed -n '2,+3p' /usr/local/peixun/content.txt
===> 输出了第2,到2+3=5行之间的行数据
hope is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,

# 正则表达式匹配的行
sed -n '/moment/p' /usr/local/peixun/content.txt
===>
The moment we lose hope is the moment we surrender our will to live.

# 从匹配到pattern1的行,到匹配到pattern2的行
sed -n '/^It/,/that is/p' /usr/local/peixun/content.txt
===>
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,

# 从指定行号开始匹配,直到匹配到pattern1
sed -n '8,/\/sbin/p' /usr/local/lamp.sh
===>
# You can install Apache + PHP + MySQL/MariaDB in an very easy way.
# Just need to input numbers to choose what you want to install before installation.
# And all things will be done in a few minutes.
#
# System Required:  CentOS 6+ / Debian 8+ / Ubuntu 14+
# Description:  Install LAMP(Linux + Apache + MySQL/MariaDB + PHP )
# Website:  https://lamp.sh
# Github:   https://github.com/teddysun/lamp

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

# 从匹配到pattern1开始,一直到指定行
sed -n  '/^show_help(/,49p' /usr/local/lamp.sh
sed -n -r '/^show_help\(/,49p' /usr/local/lamp.sh
===>
show_help(){
    echo
    echo "+-------------------------------------------------------------------+"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

sed command
command命令对照表

类别command含义
查询p (print)打印
增加a (append)行后追加
增加i (insert)行前追加
增加r (read)外部文件读入,行后追加
增加w (write)匹配行写入外部文件
删除d删除
修改s/old/new/将行内第一个old替换为new
修改s/old/new/g将行内全部的old替换为new
修改s/old/new/2仅将行内第二个的old替换为new
修改s/old/new/2g将行内第2个开始到最后的old全部替换为new
修改s/old/new/ig将行内old全部替换为new,忽略大小写
查询=显示匹配成功的行号,不显示行的内容
反向引用& 和 \1引用模式匹配到的整个字符串
查询命令含义
1p打印第一行内容
1,10p打印1-10行内容
1,+5p打印第一行及其之后5行的内容
/pattern1/p打印每行中匹配到pattern1的行内容
/pattern1/,/pattern2/p打印匹配到pattern1的行开始到匹配到pattern2的所有行
/pattern1/,10p打印匹配到pattern1的行到第10行的行内容
10,/pattern1/p打印第10行直到匹配到pattern1的所有行内容
# 删除指定的行
sed '1d' /usr/local/peixun/content.txt
===> 并没有删除文件
hope is the light that brings us comfort, faith, and reassurance.
It guides our way if we are lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is disintegrating into a vicious hatred,
where hope is needed more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in something better, something bigger than this,
is what keeps life worth living.

sed -i '1d' /usr/local/peixun/content.txt
cat -n /usr/local/peixun/content.txt
===>
     1  hope is the light that brings us comfort, faith, and reassurance.
     2  It guides our way if we are lost and gives us a foothold on our fears.
     3  The moment we lose hope is the moment we surrender our will to live.
     4  We live in a world that is disintegrating into a vicious hatred,
     5  where hope is needed more than ever but cannot be discerned.
     6  Finding that is rare while the world lives in fear,
     7  but the belief in something better, something bigger than this,
     8  is what keeps life worth living.

# 指定范围行删除
sed -i '2,4d' /usr/local/peixun/content.txt
===>
     1  hope is the light that brings us comfort, faith, and reassurance.
     2  where hope is needed more than ever but cannot be discerned.
     3  Finding that is rare while the world lives in fear,
     4  but the belief in something better, something bigger than this,
     5  is what keeps life worth living.

# 删除两个pattern之间的行
cp /usr/local/lamp.sh /usr/local/peixun/
sed -i  '/^# LAMP/,/And all/d'  /usr/local/peixun/lamp.sh
vi /usr/local/peixun/lamp.sh
set nu
===> 原来的数据删除了
4 # This file is part of the LAMP script.
5 #
6 #
7 # System Required:  CentOS 6+ / Debian 8+ / Ubuntu 14+
8 # Description:  Install LAMP(Linux + Apache + MySQL/MariaDB + PHP )
9 # Website:  https://lamp.sh
10 # Github:   https://github.com/teddysun/lamp

# a 行后追加

sed -i '/${1}/a # this is first param.' /usr/local/peixun/lamp.sh
===>
 18     local include=${1}
 19 # this is first param.

# i 行前追加
sed -i '/^cur_dir/,/^version/i # 行前追加的内容' /usr/local/peixun/lamp.sh
===>
15 # 行前追加的内容
16 cur_dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
17 # 行前追加的内容
18
19 # 行前追加的内容
20 include(){
21 # 行前追加的内容
22     local include=${1}
23 # 行前追加的内容
24 # this is first param.
25 # 行前追加的内容
26     if [[ -s ${cur_dir}/include/${include}.sh ]];then
27 # 行前追加的内容
28         . ${cur_dir}/include/${include}.sh
29 # 行前追加的内容
30     else
31 # 行前追加的内容
32         echo "Error: ${cur_dir}/include/${include}.sh not found, shell can not be executed."
33 # 行前追加的内容
34         exit 1
35 # 行前追加的内容
36     fi
37 # 行前追加的内容
38 }
39 # 行前追加的内容
40
41 # 行前追加的内容
42 version(){

# r 将指定文件的内容追加到匹配的行后面
# 文件准备 zhuijia.txt
# -------------------------
# @author kaneike
# @create_time:xxx
# @theme: 文件追加内容
# ------------------------

sed -i '1r zhuijia.txt'  /usr/local/peixun/lamp.sh
===> 在第一行后追加指定文件的内容
#!/usr/bin/env bash
# -------------------------
# @author kaneike
# @create_time:xxx
# @theme: 文件追加内容
# ------------------------

# w 将匹配的行写入指定文件中
sed -n  '/^process(/,/^}/w /usr/local/peixun/process_result.txt' /usr/local/peixun/lamp.sh
cat /usr/local/peixun/process_result.txt # 将函数process及其内容保存在文件中


# s/old/new  将行内第一个old替换为new
# 文件准备 replace.txt  cp /etc/passwd /usr/local/peixun/
sed -i 's/root/Root/' /usr/local/peixun/passwd
===>
Root:x:0:0:root:/root:/bin/bash

# s/old/new/g|将行内全部的old替换为new
sed -i 's/root/ROOT/g' /usr/local/peixun/passwd
===>
Root:x:0:0:ROOT:/ROOT:/bin/bash

# s/old/new/2  将行内匹配到的第二个进行替换
sed -i 's/ROOT/hadoop/2' /usr/local/peixun/passwd
===>
Root:x:0:0:ROOT:/hadoop:/bin/bash

# s/old/new/2g 将行内第2个开始到最后的old全部替换为new
cp -f /etc/passwd /usr/local/peixun/
sed -i 's/root/DATAX/2g'  /usr/local/peixun/passwd
===>
root:x:0:0:DATAX:/DATAX:/bin/bash

# s/old/new/ig 将行内old全部替换为new,忽略大小写
cp -i /etc/passwd /usr/local/peixun/
-- 修改user/User为------------
sed -i 's/user/------------/ig' /usr/local/peixun/passwd
===>
ftp:x:14:50:FTP ------------:/var/ftp:/sbin/nologi

# = 显示成功匹配的行数
sed -n '/root/=' /usr/local/peixun/passwd
===>  只有第1行,第10行含root
1
10

# 反向引用 & 和 \1  引用模式匹配到的整个字符串
# 文件准备 quote.txt 
hope is the hadoop hudijp that brings us comfort, faith, and reassurance.
It guides our way if we hadBBp lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp  into a vicious hatred,
where hope is hadefp more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp better, something bigger than this,
is what keeps life worth living.
# 现在想把hadxxp的数据后面加上后缀_s
sed -i 's/had..p/&_s/g' /usr/local/peixun/quote.txt 
===>  成功加撒谎给你_s
hope is the hadoop_s that brings us comfort, faith, and reassurance.
It guides our way if we hadBBp_s lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp_s  into a vicious hatred,
where hope is hadefp_s more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp_s better, something bigger than this,
is what keeps life worth living.

# 恢复文件内容
# 使用 \1 
# 将匹配到的数据替换成第二个部分+______
sed -i -r 's/(h.d)(..p)/\2_______/g' /usr/local/peixun/quote.txt
===>
hope is the oop_______ that brings us comfort, faith, and reassurance.
It guides our way if we BBp_______ lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is CCp_______  into a vicious hatred,
where hope is efp_______ more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in xjp_______ better, something bigger than this,
is what keeps life worth living.

sed -i 's/\(h.d\)\(..p\)/\2_______/g' /usr/local/peixun/quote.txt
===>
hope is the oop_______ that brings us comfort, faith, and reassurance.
It guides our way if we BBp_______ lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is CCp_______  into a vicious hatred,
where hope is efp_______ more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in xjp_______ better, something bigger than this,
is what keeps life worth living.

sed -i -r 's/(had)...../\1happy/g' /usr/local/peixun/quote.txt
===>
hope is the hadhappy that brings us comfort, faith, and reassurance.
It guides our way if we hadhappy lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadhappy  into a vicious hatred,
where hope is hadhappy more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadhappy better, something bigger than this,
is what keeps life worth living.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200

sed中引用变量时注意事项

1 匹配模式中存在变量,则建议使用双引号
sed中需要引入自定义变量时,如果外面使用单引号,则自定义变量也必须使用单引号包裹

# 重置文件 quote.txt
hope is the Hadoop hudijp that brings us comfort, faith, and reassurance.
It guides our way if we Hadoop lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp  into a vicious hatred,
where hope is hadefp more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp better, something bigger than this,
is what keeps life worth living.

#! /bin/bash
old_vari=hadoop
new_vari=Hadoop
sed -i 's/$old_vari/$new_vari/ig' /usr/local/peixun/quote.txt

#执行脚本 
sh test.sh
===>
hope is the Hadoop hudijp that brings us comfort, faith, and reassurance.
It guides our way if we Hadoop lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp  into a vicious hatred,
where hope is hadefp more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp better, something bigger than this,
is what keeps life worth living.

#! /bin/bash
old_vari=hadoop
new_vari=HADOOP
sed -i 's/'$old_vari'/'$new_vari'/ig' /usr/local/peixun/quote.txt
===>
hope is the HADOOP hudijp that brings us comfort, faith, and reassurance.
It guides our way if we HADOOP lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp  into a vicious hatred,
where hope is hadefp more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp better, something bigger than this,
is what keeps life worth living.

#! /bin/bash
old_vari=hadoop
new_vari=HADOOP
sed -i "s/$old_vari/$new_vari/ig" /usr/local/peixun/quote.txt
===>
hope is the HADOOP hudijp that brings us comfort, faith, and reassurance.
It guides our way if we HADOOP lost and gives us a foothold on our fears.
The moment we lose hope is the moment we surrender our will to live.
We live in a world that is hadCCp  into a vicious hatred,
where hope is hadefp more than ever but cannot be discerned.
Finding that is rare while the world lives in fear,
but the belief in hadxjp better, something bigger than this,
is what keeps life worth living.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

sed需求实践

计算mysql的配置文件my.ini中需要配置的服务名称,以及每个配置下有多少个配置的参数
  • 1

脚本代码

#! /bin/bash

#获取服务名称
get_segment_info(){
   echo `sed -n  '/^\[.*\]$/p' $1 | sed -e 's/\[//g' -e 's/\]//g'`
}

# 计算每个服务下有多少个配置
calcute_line_num(){
   echo `sed -n "/^\[$1]$/,/^\[/p" $2 | grep -v "^#" | grep -v "^$" | grep -v "\[" | wc -l`
}

for segment in `get_segment_info $1`
do
     echo "$segment : `calcute_line_num $segment $1`"
done

#-----------------------------------------
# 调用脚本
sh test.sh my.ini
===>
client : 1
mysql : 2
mysqld : 50
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

awk

awk是一个文本处理工具,通常用于处理数据并生成结果报告

awk的工作模式
awk的语法

1 awk 'BEGIN{}pattern{commands}END{}' file_name

2 standard output | awk 'BEGIN{}pattern{commands}END{}'
  • 1
  • 2
  • 3
语法格式解释
BEGIN{}正式处理数据之前执行
pattern匹配模式
{commands}处理命令,可能多行
END{}处理完所有匹配数据后执行
awk内置变量含义
$0整行内容
$1-$n当前行的第1-n个字段
NF当前行的字段个数,也就是多少列
NR当前行的行号,从1开始计数
FNR多文件处理时,每个文件行号单独计数,都是从0开始
FS输入字段分隔符,不指定默认以空格或tab分割
RS输入行分隔符,默认回车换行
OFS输出字段分割符,默认为空格
ORS输出行分隔符,默认为回车换行
FILENAME当前输入的文件名字
ARGC命令行参数个数
ARGV命令行参数数组
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/197347
推荐阅读
相关标签
  

闽ICP备14008679号