网站制作建设公司推荐,seo实战密码百度云,百度账号管家,微信小程序怎么制作的文章目录 前言1.Shell概述1.1概述 2.Shell解析器3.Shell脚本入门4.Shell中的变量4.1 系统变量4.2 自定义变量4.3 特殊变量#xff1a;$n4.4 特殊变量#xff1a;$#4.5 特殊变量#xff1a;\$*、$4.6 特殊变量#xff1a;$#xff1f; 5.运算符6.条件判断7.流程控制7.1 if … 文章目录 前言1.Shell概述1.1概述 2.Shell解析器3.Shell脚本入门4.Shell中的变量4.1 系统变量4.2 自定义变量4.3 特殊变量$n4.4 特殊变量$#4.5 特殊变量\$*、$4.6 特殊变量$ 5.运算符6.条件判断7.流程控制7.1 if 判断 7.2 case 语句7.3 for 循环7.4 while 循环 8.read读取控制台输入9.函数9.1 系统函数9.2 自定义函数 10.正则表达式入门9.1 常规匹配9.2 常用特殊字符 11.文本处理工具11.1 cut10.2 awk10.3 sed10.4 sort 前言 本文根据尚硅谷的课程整理而成方便各位搬砖人快速掌握Linux的Shell程序。 1.Shell概述
1.1概述 Shell脚本就是把Linux的命令封装成一个类似Java一样的编程脚本。
2.Shell解析器
#1Linux提供的Shell解析器有
[atguiguhadoop101 ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
#2bash和sh的关系
[atguiguhadoop101 bin]$ ll | grep bash
-rwxr-xr-x. 1 root root 941880 5月 11 2016 bash
lrwxrwxrwx. 1 root root 4 5月 27 2017 sh - bash
#3Centos默认的解析器是bash
[atguiguhadoop102 bin]$ echo $SHELL
/bin/bash3.Shell脚本入门
1脚本格式 脚本以#!/bin/bash开头指定解析器 2第一个Shell脚本helloworld
#1需求创建一个Shell脚本输出helloworld
[atguiguhadoop101 datas]$ touch helloworld.sh
[atguiguhadoop101 datas]$ vi helloworld.sh
#在helloworld.sh中输入如下内容
#!/bin/bash
echo helloworld3.脚本的常用执行方式 第一种采用bash或sh脚本的相对路径或绝对路径不用赋予脚本x权限
#1 sh脚本的相对路径
[atguiguhadoop101 datas]$ sh helloworld.sh
Helloworld
#2sh脚本的绝对路径
[atguiguhadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh
helloworld
#3bash脚本的相对路径
[atguiguhadoop101 datas]$ bash helloworld.sh
Helloworld
#4bash脚本的绝对路径
[atguiguhadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh
Helloworld第二种采用输入脚本的绝对路径或相对路径执行脚本必须具有可执行权限x
#1首先要赋予helloworld.sh 脚本的x权限
[atguiguhadoop101 datas]$ chmod 777 helloworld.sh
#2执行脚本
#相对路径
[atguiguhadoop101 datas]$ ./Helloworld.sh
#绝对路径
[atguiguhadoop101 datas]$ /home/atguigu/datas/helloworld.sh 注意第一种执行方法本质是bash解析器帮你执行脚本所以脚本本身不需要执行权限。第二种执行方法本质是脚本需要自己执行所以需要执行权限。 第三种用 source helloworld.sh 或者 . helloworld.sh
4第二个Shell脚本多命令处理 1需求 在/home/atguigu/目录下创建一个banzhang.txt,在banzhang.txt文件中增加“I love cls”。
[atguiguhadoop101 datas]$ touch batch.sh
[atguiguhadoop101 datas]$ vi batch.sh
#在batch.sh中输入如下内容#!/bin/bashcd /home/atguigu
touch cls.txt
echo I love cls cls.txt4.Shell中的变量
4.1 系统变量
1 常用系统变量
$HOME、$PWD、$SHELL、$USER等#1查看系统变量的值
[atguiguhadoop101 datas]$ echo $HOME
/home/atguigu
#2显示当前Shell中所有变量set
[atguiguhadoop101 datas]$ set
BASH/bin/bash
BASH_ALIASES()
BASH_ARGC()
BASH_ARGV()4.2 自定义变量
1基本语法 1定义变量变量值 2撤销变量unset 变量 3声明静态变量readonly变量注意不能unset 2变量定义规则 1变量名称可以由字母、数字和下划线组成但是不能以数字开头环境变量名建议大写。 2等号两侧不能有空格 3在bash中变量默认类型都是字符串类型无法直接进行数值运算。 4变量的值如果有空格需要使用双引号或单引号括起来。
#1定义变量A
[atguiguhadoop101 datas]$ A5
[atguiguhadoop101 datas]$ echo $A
5
#2给变量A重新赋值
[atguiguhadoop101 datas]$ A8
[atguiguhadoop101 datas]$ echo $A
8
#3撤销变量A
[atguiguhadoop101 datas]$ unset A
[atguiguhadoop101 datas]$ echo $A
#4声明静态的变量B2不能unset
[atguiguhadoop101 datas]$ readonly B2
[atguiguhadoop101 datas]$ echo $B
2
[atguiguhadoop101 datas]$ B9
-bash: B: readonly variable
#5在bash中变量默认类型都是字符串类型无法直接进行数值运算
[atguiguhadoop102 ~]$ C12
[atguiguhadoop102 ~]$ echo $C
12
#6变量的值如果有空格需要使用双引号或单引号括起来
[atguiguhadoop102 ~]$ DI love banzhang
-bash: world: command not found
[atguiguhadoop102 ~]$ DI love banzhang
[atguiguhadoop102 ~]$ echo $A
I love banzhang
#7可把变量提升为全局环境变量可供其他Shell程序使用
export 变量名
[atguiguhadoop101 datas]$ vim helloworld.sh
#在helloworld.sh文件中增加echo $B
#!/bin/bashecho helloworld
echo $B#[atguiguhadoop101 datas]$ ./helloworld.sh
Helloworld
#发现并没有打印输出变量B的值。
[atguiguhadoop101 datas]$ export B
[atguiguhadoop101 datas]$ ./helloworld.sh
helloworld
24.3 特殊变量$n
1基本语法 $n 功能描述n为数字$0代表该脚本名称$1-$9代表第一到第九个参数十以上的参数十以上的参数需要用大括号包含如${10} 2案例实操 1输出该脚本文件名称、输入参数1和输入参数2 的值
[atguiguhadoop101 datas]$ touch parameter.sh
[atguiguhadoop101 datas]$ vim parameter.sh#!/bin/bash
echo $0 $1 $2
[atguiguhadoop101 datas]$ chmod 777 parameter.sh
[atguiguhadoop101 datas]$ ./parameter.sh cls xz
./parameter.sh cls xz4.4 特殊变量$#
1基本语法 $# 功能描述获取所有输入参数个数常用于循环。 2案例实操 1获取输入参数的个数
atguiguhadoop101 datas]$ vim parameter.sh
#!/bin/bash
echo $0 $1 $2
echo $#[atguiguhadoop101 datas]$ chmod 777 parameter.sh
[atguiguhadoop101 datas]$ ./parameter.sh cls xz
parameter.sh cls xz
24.5 特殊变量$*、$
1基本语法 $*功能描述这个变量代表命令行中所有的参数$*把所有的参数看成一个整体 $功能描述这个变量也代表命令行中所有的参数不过$把每个参数区分对待 2案例实操 1打印输入的所有参数
[atguiguhadoop101 datas]$ vim parameter.sh#!/bin/bash
echo $0 $1 $2
echo $#
echo $*
echo $[atguiguhadoop101 datas]$ bash parameter.sh 1 2 3
parameter.sh 1 2
3
1 2 3
1 2 34.6 特殊变量$
1基本语法 $ 功能描述最后一次执行的命令的返回状态。如果这个变量的值为0证明上一个命令正确执行如果这个变量的值为非0具体是哪个数由命令自己来决定则证明上一个命令执行不正确了。 2案例实操 1判断helloworld.sh脚本是否正确执行
[atguiguhadoop101 datas]$ ./helloworld.sh
hello world
[atguiguhadoop101 datas]$ echo $?
05.运算符
1基本语法 1“$((运算式))”或“$[运算式]” 2expr , - , *, /, % 加减乘除取余 注意expr运算符间要有空格 2案例实操
#1计算32的值
[atguiguhadoop101 datas]$ expr 2 3
5
#2计算3-2的值
[atguiguhadoop101 datas]$ expr 3 - 2
1
#3计算23X4的值
#aexpr一步完成计算
[atguiguhadoop101 datas]$ expr expr 2 3 \* 4
20
#b采用$[运算式]方式
[atguiguhadoop101 datas]# S$[(23)*4]
[atguiguhadoop101 datas]# echo $S6.条件判断
1基本语法 [ condition ]注意condition前后要有空格 注意条件非空即为true[ atguigu ]返回true[] 返回false。 2. 常用判断条件 1两个整数之间比较 字符串比较 -lt 小于less than -le 小于等于less equal -eq 等于equal -gt 大于greater than -ge 大于等于greater equal -ne 不等于Not equal 2按照文件权限进行判断 -r 有读的权限read -w 有写的权限write -x 有执行的权限execute 3按照文件类型进行判断 -f 文件存在并且是一个常规的文件file -e 文件存在existence -d 文件存在并是一个目录directory 3案例实操
#123是否大于等于22
[atguiguhadoop101 datas]$ [ 23 -ge 22 ]
[atguiguhadoop101 datas]$ echo $?
0
#2helloworld.sh是否具有写权限
[atguiguhadoop101 datas]$ [ -w helloworld.sh ]
[atguiguhadoop101 datas]$ echo $?
0
#3/home/atguigu/cls.txt目录中的文件是否存在
[atguiguhadoop101 datas]$ [ -e /home/atguigu/cls.txt ]
[atguiguhadoop101 datas]$ echo $?
1
#4多条件判断 表示前一条命令执行成功时才执行后一条命令|| 表示上一条命令执行失败后才执行下一条命令
[atguiguhadoop101 ~]$ [ condition ] echo OK || echo notok
OK
[atguiguhadoop101 datas]$ [ condition ] [ ] || echo notok
notok7.流程控制
7.1 if 判断
1基本语法 if [ 条件判断式 ];then 程序 fi 或者 if [ 条件判断式 ] then 程序 fi 注意事项 1[ 条件判断式 ]中括号和条件判断式之间必须有空格 2if后要有空格 2案例实操
#1输入一个数字如果是1则输出banzhang zhen shuai如果是2则输出cls zhen mei如果是其它什么也不输出。
[atguiguhadoop101 datas]$ touch if.sh
[atguiguhadoop101 datas]$ vim if.sh#!/bin/bashif [ $1 -eq 1 ]
thenecho banzhang zhen shuai
elif [ $1 -eq 2 ]
thenecho cls zhen mei
fi[atguiguhadoop101 datas]$ chmod 777 if.sh
[atguiguhadoop101 datas]$ ./if.sh 1
banzhang zhen shuai7.2 case 语句
1基本语法 case $变量名 in “值1” 如果变量的值等于值1则执行程序1 ;; “值2” 如果变量的值等于值2则执行程序2 ;; …省略其他分支… 如果变量的值都不是以上的值则执行此程序 ;; esac 注意事项 1case行尾必须为单词“in”每一个模式匹配必须以右括号“”结束。 2双分号“;;”表示命令序列结束相当于java中的break。 3最后的“”表示默认模式相当于java中的default。 2案例实操
1输入一个数字如果是1则输出banzhang如果是2则输出cls如果是其它输出renyao。
[atguiguhadoop101 datas]$ touch case.sh
[atguiguhadoop101 datas]$ vim case.sh!/bin/bashcase $1 in
1)echo banzhang
;;
2)echo cls
;;
*)echo renyao
;;
esac[atguiguhadoop101 datas]$ chmod 777 case.sh
[atguiguhadoop101 datas]$ ./case.sh 1
17.3 for 循环
1基本语法1 for (( 初始值;循环控制条件;变量变化 )) do 程序 done 2案例实操
#1从1加到100
[atguiguhadoop101 datas]$ touch for1.sh
[atguiguhadoop101 datas]$ vim for1.sh#!/bin/bashs0
for((i0;i$1;i))
dos$[$s$i]
done
echo $s[atguiguhadoop101 datas]$ chmod 777 for1.sh
[atguiguhadoop101 datas]$ ./for1.sh 100
“5050”3基本语法2 for 变量 in 值1 值2 值3… do 程序 done 4案例实操
#1打印所有输入参数
[atguiguhadoop101 datas]$ touch for2.sh
[atguiguhadoop101 datas]$ vim for2.sh#!/bin/bash
#打印数字for i in $*doecho ban zhang love $i done[atguiguhadoop101 datas]$ chmod 777 for2.sh
[atguiguhadoop101 datas]$ bash for2.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd#2比较$*和$区别
#a$*和$都表示传递给函数或脚本的所有参数不被双引号“”包含时都以$1 $2 …$n的形式输出所有参数。
[atguiguhadoop101 datas]$ touch for.sh
[atguiguhadoop101 datas]$ vim for.sh#!/bin/bash for i in $*
doecho ban zhang love $i
donefor j in $
do echo ban zhang love $j
done[atguiguhadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd
ban zhang love cls
ban zhang love xz
ban zhang love bd#b当它们被双引号“”包含时“$*”会将所有的参数作为一个整体以“$1 $2 …$n”的形式输出所有参数“$”会将各个参数分开以“$1” “$2”…”$n”的形式输出所有参数。
[atguiguhadoop101 datas]$ vim for.sh#!/bin/bash for i in $*
#$*中的所有参数看成是一个整体所以这个for循环只会循环一次 do echo ban zhang love $idone for j in $
#$中的每个参数都看成是独立的所以“$”中有几个参数就会循环几次 do echo ban zhang love $j
done[atguiguhadoop101 datas]$ chmod 777 for.sh
[atguiguhadoop101 datas]$ bash for.sh cls xz bd
ban zhang love cls xz bd
ban zhang love cls
ban zhang love xz
ban zhang love bd7.4 while 循环
1基本语法 while [ 条件判断式 ] do 程序 done 2案例实操
#1从1加到100
[atguiguhadoop101 datas]$ touch while.sh
[atguiguhadoop101 datas]$ vim while.sh#!/bin/bash
s0
i1
while [ $i -le 100 ]
dos$[$s$i]i$[$i1]
doneecho $s[atguiguhadoop101 datas]$ chmod 777 while.sh
[atguiguhadoop101 datas]$ ./while.sh
50508.read读取控制台输入
1基本语法 read(选项)(参数) 选项 -p指定读取值时的提示符 -t指定读取值时等待的时间秒。 参数 变量指定读取值的变量名 2案例实操
#1提示7秒内读取控制台输入的名称
[atguiguhadoop101 datas]$ touch read.sh
[atguiguhadoop101 datas]$ vim read.sh#!/bin/bashread -t 7 -p Enter your name in 7 seconds NAME
echo $NAME[atguiguhadoop101 datas]$ ./read.sh
Enter your name in 7 seconds xiaoze
xiaoze9.函数
9.1 系统函数
1basename基本语法 basename [string / pathname] [suffix] 功能描述basename命令会删掉所有的前缀包括最后一个‘/’字符然后将字符串显示出来。 选项 suffix为后缀如果suffix被指定了basename会将pathname或string中的suffix去掉。 2案例实操
#1截取该/home/atguigu/banzhang.txt路径的文件名称
[atguiguhadoop101 datas]$ basename /home/atguigu/banzhang.txt
banzhang.txt
[atguiguhadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhangdirname基本语法 dirname 文件绝对路径 功能描述从给定的包含绝对路径的文件名中去除文件名非目录的部分然后返回剩下的路径目录的部分 4案例实操
#1获取banzhang.txt文件的路径
[atguiguhadoop101 ~]$ dirname /home/atguigu/banzhang.txt
/home/atguigu9.2 自定义函数
1基本语法 [ function ] funname[()] { Action; [return int;] } funname 2经验技巧 1必须在调用函数地方之前先声明函数shell脚本是逐行运行。不会像其它语言一样先编译。 2函数返回值只能通过$?系统变量获得可以显示加return返回如果不加将以最后一条命令运行结果作为返回值。return后跟数值n(0-255) 3案例实操
#1计算两个输入参数的和
[atguiguhadoop101 datas]$ touch fun.sh
[atguiguhadoop101 datas]$ vim fun.sh#!/bin/bash
function sum()
{s0s$[ $1 $2 ]echo $s
}read -p Please input the number1: n1;
read -p Please input the number2: n2;
sum $n1 $n2;[atguiguhadoop101 datas]$ chmod 777 fun.sh
[atguiguhadoop101 datas]$ ./fun.sh
Please input the number1: 2
Please input the number2: 5
7#2计算两个输入参数的和
#!/bin/bash
function add(){s$[ $1 $2 ]echo $s
}
read -p 请输入第一个整数 a
read -p 请输入第二个整数 b
sum$(add $a $b)
echo 和为$a
echo 和的平方为$[$sum * $sum][atguiguhadoop101 datas]$ chmod 777 add.sh
[atguiguhadoop101 datas]$ ./add.sh
请输入第一个整数 2
请输入第二个整数 5
和为7
和的平方为4910.正则表达式入门 正则表达式使用单个字符串来描述、匹配一系列符合某个语法规则的字符串。在很多文本编辑器里正则表达式通常被用来检索、替换那些符合某个模式的文本。在 Linux 中grepsedawk 等文本处理工具都支持通过正则表达式进行模式匹配。
9.1 常规匹配
#一串不包含特殊字符的正则表达式匹配它自己例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep atguigu
#就会匹配所有包含 atguigu 的行。9.2 常用特殊字符
1特殊字符^
#^ 匹配一行的开头例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep ^a
#会匹配出所有以 a 开头的行2特殊字符$
#$ 匹配一行的结束例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep t$
#会匹配出所有以 t 结尾的行
#思考^$ 匹配什么3特殊字符.
#. 匹配一个任意的字符例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep r..t
#会匹配包含 rabt,rbbt,rxdt,root 等的所有行4特殊字符*
# 不单独使用他和上一个字符连用表示匹配上一个字符 0 次或多次例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep ro*t
#会匹配 rt, rot, root, rooot, roooot 等所有行
#思考.* 匹配什么5字符区间中括号[ ] [ ] 表示匹配某个范围内的一个字符例如 [6,8]------匹配 6 或者 8 [0-9]------匹配一个 0-9 的数字 [0-9]------匹配任意长度的数字字符串 [a-z]------匹配一个 a-z 之间的字符 [a-z] ------匹配任意长度的字母字符串 [a-c, e-f]-匹配 a-c 或者 e-f 之间的任意字符
[atguiguhadoop101 shells]$ cat /etc/passwd | grep r[a,b,c]*t
#会匹配 rt,rat, rbt, rabt, rbact,rabccbaaacbt 等等所有行6特殊字符\ \ 表示转义并不会单独使用。由于所有特殊字符都有其特定匹配模式当我们想匹配某一特殊字符本身时例如我想找出所有包含 ‘$’ 的行就会碰到困难。此时我们就要将转义字符和特殊字符连用来表示特殊字符本身例如
[atguiguhadoop101 shells]$ cat /etc/passwd | grep a\$b
#就会匹配所有包含 a$b 的行。注意需要使用单引号将表达式引起来。11.文本处理工具
11.1 cut cut 的工作就是“剪”具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。 1基本用法 cut [选项参数] filename 说明默认分隔符是制表符 2选项参数说明
#1数据准备
[atguiguhadoop101 shells]$ touch cut.txt
[atguiguhadoop101 shells]$ vim cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
#2切割 cut.txt 第一列
[atguiguhadoop101 shells]$ cut -d -f 1 cut.txt
dong
guan
wo
lai
le
#3切割 cut.txt 第二、三列
[atguiguhadoop101 shells]$ cut -d -f 2,3 cut.txt
shen
zhen
wo
lai
le
#4在 cut.txt 文件中切割出 guan
[atguiguhadoop101 shells]$ cat cut.txt |grep guan | cut -d -f 1
guan
#5选取系统 PATH 变量值第 2 个“”开始后的所有路径
[atguiguhadoop101 shells]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/atguigu/.local/bin:/
home/atguigu/bin
[atguiguhadoop101 shells]$ echo $PATH | cut -d : -f 3-
/usr/local/sbin:/usr/sbin:/home/atguigu/.local/bin:/home/atguigu/bin
#6切割 ifconfig 后打印的 IP 地址
[atguiguhadoop101 shells]$ ifconfig ens33 | grep netmask | cut -d -f 10
192.168.111.10110.2 awk 一个强大的文本分析工具把文件逐行的读入以空格为默认分隔符将每行切片切开的部分再进行分析处理。 1基本用法 awk [选项参数] ‘/pattern1/{action1} /pattern2/{action2}…’ filename pattern表示 awk 在数据中查找的内容就是匹配模式 action在找到匹配内容时所执行的一系列命令 2选项参数说明 -F 指定输入文件分隔符 -v 赋值一个用户定义变量 3案例实操
#1数据准备
[atguiguhadoop101 shells]$ sudo cp /etc/passwd ./
#passwd 数据的含义
#用户名:密码(加密过后的):用户 id:组 id:注释:用户家目录:shell 解析器
#2搜索 passwd 文件以 root 关键字开头的所有行并输出该行的第 7 列。
[atguiguhadoop101 shells]$ awk -F : /^root/{print $7} passwd
/bin/bash
#3搜索 passwd 文件以 root 关键字开头的所有行并输出该行的第 1 列和第 7 列中间以“”号分割。
[atguiguhadoop101 shells]$ awk -F : /^root/{print $1,$7} passwd
root,/bin/bash
#注意只有匹配了 pattern 的行才会执行 action。
#4只显示/etc/passwd 的第一列和第七列以逗号分割且在所有行前面添加列名 usershell 在最后一行添加dahaige/bin/zuishuai。
[atguiguhadoop101 shells]$ awk -F : BEGIN{print user, shell} {print $1,$7}
END{print dahaige,/bin/zuishuai} passwd
user, shell
root,/bin/bash
bin,/sbin/nologin 。。。
atguigu,/bin/bash
dahaige,/bin/zuishuai
#注意BEGIN 在所有数据读取行之前执行END 在所有数据执行之后执行。
#5将 passwd 文件中的用户 id 增加数值 1 并输出
[atguiguhadoop101 shells]$ awk -v i1 -F : {print $3i} passwd
1
2
3
44awk 的内置变量 5案例实操
#1统计 passwd 文件名每行的行号每行的列数
[atguiguhadoop101 shells]$ awk -F : {print filename: FILENAME ,linenum:NR,col:NF} passwd
filename:passwd,linenum:1,col:7
filename:passwd,linenum:2,col:7
filename:passwd,linenum:3,col:7
#2查询 ifconfig 命令输出结果中的空行所在的行号
[atguiguhadoop101 shells]$ ifconfig | awk /^$/{print NR}
9
18
26
#3切割 IP
[atguiguhadoop101 shells]$ ifconfig ens33 | awk /netmask/ {print $2}
192.168.6.10110.3 sed sed是一种流编辑器它一次处理一行内容。处理时把当前处理的行存储在临时缓冲区中称为“模式空间”接着用sed命令处理缓冲区中的内容处理完成后把缓冲区的内容送往屏幕。接着处理下一行这样不断重复直到文件末尾。文件内容并没有改变除非你使用重定向存储输出。 1.基本用法 sed [选项参数] ‘command’ filename 2.选项参数说明 -e 直接在指令列模式上进行sed的动作编辑。 3.命令功能描述 4.案例实操
#1数据准备
[atguiguhadoop102 datas]$ touch sed.txt
[atguiguhadoop102 datas]$ vim sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
#2将“mei nv”这个单词插入到sed.txt第二行下打印。
[atguiguhadoop102 datas]$ sed 2a mei nv sed.txt
dong shen
guan zhen
mei nv
wo wo
lai lai
le le
[atguiguhadoop102 datas]$ cat sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
#注意文件并没有改变
#2删除sed.txt文件所有包含wo的行
[atguiguhadoop102 datas]$ sed /wo/d sed.txt
dong shen
guan zhen
lai lai
le le
3将sed.txt文件中wo替换为ni
[atguiguhadoop102 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
[atguiguhadoop102 datas]$ sed -e 2d -e s/wo/ni/g sed.txt
dong shen
ni ni
lai lai
le le10.4 sort sort命令是在Linux里非常有用它将文件进行排序并将排序结果标准输出。 1.基本语法 sort(选项)(参数) 参数指定待排序的文件列表 2. 案例实操
#1数据准备
[atguiguhadoop102 datas]$ touch sort.sh
[atguiguhadoop102 datas]$ vim sort.sh
bb:40:5.4
bd:20:4.2
xz:50:2.3
cls:10:3.5
ss:30:1.6
#2按照“”分割后的第三列倒序排序。
[atguiguhadoop102 datas]$ sort -t : -nrk 3 sort.sh
bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3
ss:30:1.6