Shell学习笔记

shell学习笔记

shell 条件测试和判断语句

ex4-24.sh

#! /bin/sh

echo "please enter a score:"

read score

if [ -z "$score" ];then
    echo "you enter nothing.please enter a score:"
    read score
else
    if [ "$score" -lt 0 -o "score" -gt 100 ];then
        echo "the score should be between 0 and 100.please enter again:"
        read score
    else
        #如果成绩大于90
        if [ "$score" -ge 90 ];then
            echo "the grade is A."
        #如果成绩大于80且小于90
        elif [ "$score" -ge 80 ];then
            echo "the grade is B."
        #如果成绩大于70且小于80
        elif [ "$score" -ge 70 ];then
            echo "the grade is C."
        #如果成绩大于60且小于70
        elif [ "$score" -ge 60 ];then
            echo "the grade is D."
        #如果成绩小于60
        else
            echo "the grade is E."
        fi
    fi
fi

使用exit语句退出程序

vim ex4-25.sh

#! /bin/sh
#使用echo语句输出字符串
echo hello world!
#使用$?获取echo语句执行状态
echo $?
#执行一个无效的命令
aaa
#输出执行状态
echo $?
#退出
exit 120

vim ex4-26.sh

#! /bin/sh
#如果文件已经存在,则直接退出
if [ -e "$1" ]
then
    echo "file $1 exists."
    exit 1
#如果文件不存在
else
    touch "$1"
    echo "file $1 has been created."
    exit 0
fi

##多条件判断语句case
#利用case语句处理用户输入

vim ex4-27.sh

#! /bin/sh
#输出提示信息
echo "hit a key,then hit return."
#读取用户按下的键
read keypress
#case 语句开始
case "$keypress" in
    #小写字母
    [[:lower:]])
        echo "lowercase lettter.";;
    #大写字母
    [[:upper:]])
        echo "uppercase letter.";;
    #单个数字
    [0-9])
        echo "digit.";;
    #其他字符
    *)
        echo "other letter.";;
esac

##运算符
vim ex4-28.sh

#! /bin/sh
#计算2和100的差,即98
result=`expr 2 - 100`
echo "$result"
#计算2和100的和,即102
result=`expr 2 + 100`
echo "$result"
#计算2和5的乘积
result=`expr 2 \* 5`
echo "$result"
#计算24和8的商,即3
result=`expr 24 / 8`
echo "$result"
#计算2和6的差,然后再乘以12,即-48
result=`expr \( 2 - 6 \) \* 12`
echo "$result"
#错误的语法
result=`expr 2+5`
echo "$result"
result1=`expr 2-4*9`
echo "$result1"
result2=`expr 1-(4-7)`
echo "$result2"

#使用 $((…))
#这种形式无需对运算符和括号做转义处理
vim ex4-29.sh

#! /bin/sh
#紧凑格式。计算3和6的和
result=$((3+6))
echo "$result"
#松散格式,计算3和6的和
result1=$(( 3 + 9 ))
echo "$result1"
result2=$(( 3 * 6 ))
echo "$result2"
result3=$(( 7 / 5 ))
echo "$result3"
result4=$(( 8 % 3 ))
echo "$result4"
#复合运算
result5=$(( ( 1 - 4 ) * 5 ))
echo "$result5"
result6=$(( ( 5 % 3 ) ** 2 ))
echo "$result6"

#使用 $[…]
#效果和使用两个圆括号一样

vim ex4-30.sh

#! /bin/sh
#加法运算
result=$[4+5]
echo "$result"
#复合运营   
result1=$[(1+2)*3]
echo "$result1"
result2=$[2**4]
echo "$result2"

result3=$[ 2 ** 10]
echo "$result3"
result4=$[ 2 ** 20]
echo "$result4"

#使用let命令
vim ex4-31.sh
#! /bin/sh
#定义变量
n=10
#加法运算
let n=n+1
echo "$n"QAZQA
#乘法运算
let n=n+10
echo "$n"
#幂运算
let n=n**2
echo "$n"

#位运算符

20240826

day1

shell学习 第六章 函数

6.1 函数

6.1.1 什么是函数

6.1.2 函数的定义

vim ex6-1.sh

#! /bin/bash

#定义函数
function sayhello()
{
    echo "Hello,World!"
}
#调用函数
sayhello

bash ex6-1.sh

vim ex6-1-v2.sh

#! /bin/bash

#定义函数
sayhello()
{
    echo "Hello,World!"
}
#调用函数
sayhello

bash ex6-1-v2.sh

6.1.3 函数的调用

vim ex6-2.sh

#! /bin/bash

#定义函数
getCurrentTime()
{
    current_time=`date`
    echo "${current_time}"
}
#调用函数
getCurrentTime

bash ex6-2.sh

6.1.4 函数链接

vim ex6-3.sh

#!  /bin/bash

#定义函数
john()
{
    echo "Hello.this is john!"
}
#定义函数 alice
alice()
{
    #调用函数john
    john
    echo "hello,this is Alice"
}
#调用函数alice
alice

bash ex6-3.sh

某个函数同时调用多个其他函数的方法

vim ex6-4.sh

#! /bin/bash

#定义函数
john()
{
    echo "hello.this is john"
}
#定义函数alice
alice()
{
    echo "hello,this is Alice"
}
#定义函数sayhello
sayhello()
{
    john
    alice
    echo "Hello,World!"
}
#调用函数sayhello
sayhello

bash ex6-4.sh

6.1.5函数返回值

如何通过return返回某个数值

vim ex6-5.sh

#! /bin/bash

#定义求和函数
sum()
{
    let "z = $1 + $2 "
    #将和作为退出状态返回
    return "$z"
}
#调用求和函数
sum 22 4
#输出和
echo "$?"

bash ex6-5.sh

############################################

attention # return 只能返回0~255之间的数值

############################################

#如何通过echo来传递函数返回值

vim ex6-6.sh

#! /bin/bash

#定义函数
length()
{
    #接收参数
    str=$1
    result=0
    if [ "${str}" != "" ];then
        #计算字符串长度
        result=${#str}
    fi
    #将长度值写入标准输出
    echo "${result}"
}
#调用函数
len=$(length "abc123")
#输出执行结果
echo "the string's length is $len"

bash ex6-6.sh

20240827

day2

6.1.6 函数和别名

#定义别名
alias ls=“ls -l”
ls
#删除别名
unalias ls
#删除函数
unset lsl

#再议全局变量和局部变量
#在函数内部,如果没有用loacal关键字来修饰,那么函数变量也是全局变量

vim ex6-7.sh

#! /bin/bash
var="Hello world"

func()
{
    #在函数内改变变量的值
    var="Orange Apple Banana"
    echo "$var"
    #在函数内定义全局变量
    var2="Hello john"
}
#输出变量值
echo "$var"
#调用函数
func
#重新输出变量的值
echo "$var"
#输出函数内定义的变量的值
echo "$var2"

#演示在函数内部定义局部变量的方法

vim ex6-8.sh

#! /bin/bash
#全局变量
var="Hello world!"
func()
{
    #局部变量
    local var="Orange Apple Banana"
    echo "$var"
    #局部变量
    local var2="nice to meet you."
}
echo "$var"
func
echo "$var"
echo "$var2"

6.2 函数参数

6.2.1 含有参数的函数调用方法

实参:函数调用时指定的参数

形参:函数定义时指定的参数

6.2.2 获取函数参数的个数

vim ex6-9.sh

#! /bin/bash
#定义函数
func()
{
    #输出参数个数
    echo "the function has $# parameters."
}
#调用函数
func a b c d e f g hello
func 12 3 "hello world"
func

6.2.3 通过位置变量接受参数值

vim ex6-10.sh

#! /bin/bash
#定义函数
func()
{
    #输出所有的参数
    echo "all parameters are $*"
    echo "all parameters are $@"
    #输出脚本名称
    echo "the script's name is $0"
    #输出第1个参数
    echo "the first parameters is $1"
    #输出第2个参数
    echo "then second parameters is $2"
}
#调用函数
func hello world

6.2.4 移动位置参数

依次获取传递给函数的每个参数

vim ex6-11.sh

#! /bin/bash
#定义函数
func()
{
    #通过while循环和shift命令依次获取参数
    #获取函数参数的个数
    while (($# > 0))
    do
        echo "$1"
        #将原来参数删除,同时位置参数向左移动一个位置
        shift
    done
}

6.2.5 通过getopts接收函数参数

vim ex6-12.sh

#! /bin/bash
#定义函数
func()
{
    #逐个接受选项和参数
    while getopts "a:b:c" arg
    do
        #当指定-a选项是
        case "$arg" in
            a)
                #输出-a选项的参数
                echo "a's argument is $OPTARG."
                ;;
            b)
                echo "b's argument is $OPTARG."
                ;;
            c)
                echo "c"
                ;;
            ?)
                #未知选项
                echo "unkown argument"
                exit 1
                ;;
        esac
    done
}
#调用函数
func -a hello -b world -c nice

6.2.6 间接参数传递

如果某个变量的值又是另一个变量的变量名,则改变量为间接变量

vim ex6-13.sh

#! /bin/bash

#定义函数
func()
{
    echo "$1"
}
#定义变量
var=name
name=John
#调用函数
func "$var"
func ${!var}
#修改变量的值
name=Alice
#再次调用函数
func "$var"
func ${!var}

6.2.7 通过全局变量传递数据

vim ex6-14.sh

#! /bin/bash

#定义全局变量
file="/bin/ls"
#定义函数
func()
{
    if [ -e "$file" ]
    then
        echo "the file exists."
    else
        echo "the file does not exists."
    fi
}
#调用函数
func
#修改全局变量
file="/bin/a"
#调用函数
func

6.2.8 传递数组参数

将数组元素作为多个参数传递给函数

vim ex6-15.sh

#! /bin/bash

#定义函数
func()
{
    echo "number of elemennt is $#."
    while [ $# -gt 0 ]
    do
        echo "$1"
        shift
    done
}
#定义数组
a=(a b "c d" e)
#调用函数 
#${a[@]}获取数组所有元素
func "${a[@]}"
echo "~~~~~~~~"
#去掉双引号效果
func ${a[@]}

20240828

day3

6.3 函数库文件

6.3.1 函数库文件的定义

下面定义一个函数库文件

vim lib.sh

#! /bin/bash

#定义函数
error()
{
    echo "ERROR:" $@ 1>&2
}
warning()
{
    echo "WARNING:" $@ 1>&2
}

6.3.2 函数库文件的调用

函数库文件的调用方法 . filename

vim ex6-16.sh

#! /bin/bash

#载入函数库
. ex6-22.sh

#定义变量
msg="the file is not found."
#调用函数库中的函数
error $msg

6.4 递归函数

举例:该函数慎用,会无限循环下去

vim ex6-17.sh

#! /bin/bash
#定义递归函数
func()
{
    read y
    #递归调用
    func "$y"
    echo "$y"
}
#调用函数
func

#计算 n的阶乘

vim ex6-18.sh

#! /bin/bash
#定义递归函数
fact()
{
    #定义局部变量
    local n="$1"
    #当n等于0时终止递归调用
    if [ "$n" -eq 0 ]
    then 
        result=1
    else
        #当n大于0时,递归计算n-1的阶乘
        let "m=n-1"
        fact "$m"
        let "result=$n * $?"
    fi
    #将计算结果以退出状态码的形式返回
    return $result
}
#调用递归函数
fact "$1"

echo "Factorial of $1 is $?"

20240829

day4

shell学习 第七章 数组

7.1 定义数组

7.1.1 通过指定元素来定义数组

vim ex7-1.sh

#! /bin/bash

#指定数组元素值
array[1]=one
array[2]=three

#输出数组元素
echo "${array[@]}"

7.1.2 通过declare语句定义数组

vim ex7-2.sh

#! /bin/bash

#定义数组
declare -a array
#为元素赋值
array[0]=1
array[1]=2
#输出元素值
echo "${array[@]}"

7.1.3 通过元素值集合定义数组

vim ex7-3.sh

#! /bin/bash

#定义数组
array=(1 2 3 4 5 6 7 8 9)
#输出第一个数组元素的值
echo "the first element is ${#array[0]}"
#输出所有元素的值
echo "the element of this array are ${array[@]}"
#输出数组长度
echo "the length of the array is ${#array[@]}"

7.1.4 通过键值对定义数组

vim ex7-4.sh

#! /bin/bash

#定义数组
array=([1]=one [3]=three)
#输出数组长度
echo "the lengh of array is ${#array[@]}"
#输出索引为4的元素的值
echo "the fourth element is ${array[4]}"

vim ex7-5.sh

#! /bin/bash

#声明数组
declare -A array
#为数组赋值
array=([flower]=rose [fruit]=apple)
#输出第一个元素的值
echo "the first value of array is ${array[flower]}"
echo "the first value of array is ${array[0]}"
#输出第2个元素的值
echo "the fruit is ${array[fruit]}"
echo "the fruit is ${array[1]}"
#输出数组长度
echo "the size of the array is ${#array[@]}"

7.1.5 数组和普通变量

vim ex7-6.sh

#! /bin/bash

#定义字符串变量
array="hello.world."
#输出下标为0的元素的值
echo "${array[0]}"
#输出所有元素的值
echo "${array[@]}"
echo "${#array[@]}"
echo "${array[*]}"

7.2 数组的赋值

7.2.1 按索引为元素赋值

vim ex7-7.sh

#! /bin/bash

#定义数组
students=(John Rose Tom Tim)
#输出元素值
echo "the old students are: ${students[*]}"
#改变第一个元素的值
students[0]=Suan
#改变第4个元素的值
students[3]=Jack
#输出数组新的值
echo "the new students are: ${students[*]}"
#声明关联数组
declare -A grades
#初始化新的数组
grades=([john]=90 [rose]=87 [tim]=78 [tom]=58 [jsck]=76)
#输出数组元素
echo "the grade are: ${grades[*]}"
#改变tim的分数
grades[tim]=100
#重新输出数组
echo "the new grades are: ${grades[*]}"

7.2.2 通过集合为数组赋值

vim ex7-8.sh

#! /bin/bash

#定义数组
a=(a b c def)
#输出所有元素的值
echo "${a[@]}"
echo "${a[*]}"
echo "${a}" #输出第一个元素的值
echo "$a" #输出第一个元素的值
#为数组元素赋值
a=(h i j k l)
#输出所有数组元素的值
echo "${a[*]}"
echo "${a[@]}"
#为数组元素重新赋值
a=(m n)
echo "$a[@]"
echo "${a[@]}"
echo "${a[*]}"

7.2.2 在数组末尾追加新元素

vim ex7-9.sh

#! /bin/bash

#定义数组
array=(1 2)
#输出数组
echo "${array[*]}"
echo "${array[@]}"
#向数组末尾追加元素
array[2]=3
array[3]=100
echo "${array[@]}"
echo "${array[*]}"
echo "${#array[@]}"
echo "${#array[*]}"

#向关联数组追加新元素

vim ex7-10.sh

#! /bin/bash

#定义数组
declare -A array
#初始化新的数组
array=([a]=-a [b]=-b)
echo "the old elemennt are ${array[*]}"
#追加元素
array[c]=case
echo "the new elemennt are ${array[@]}"
echo "end"

vim ex7-10_2.sh

#! /bin/bash

#定义数组
declare -A array
#初始化新的数组
array=([a]=a [b]=b)
echo "the old elemennt are ${array[@]}"
#追加元素
array[c]=c
echo "the new elemennt are ${array[@]}"
echo "end"

20240830

day5

7.3 访问数组

7.3.1 访问第一个数组元素

vim ex7-12.sh

#! /bin/bash

#定义数组
array=(1 2 3 4 5)
#通过数组名访问数组
echo "${array}"

7.3.2 通过下标访问数组

vim ex7-13.sh

#! /bin/bash

#定义数组
array=(Mon Tue Wed Thu Fir Sat Sun)
#输出下标为0的元素的值
echo "the first element is ${array[0]}"
#输出下标为3的元素
echo "the fourth element is ${array[3]}"

7.3.3 计算数组长度

${#array[@]} ${array[*]}

vim ex7-14.sh

#! /bin/bash

#定义数组
array=(Mon Tue Wed Thu Fir Sat Sun)
#输出数组长度
echo "the length of array is ${#array[*]}"
echo "the length of array is ${#array[@]}"

vim ex7-15.sh

#! /bin/bash
#定义数组
linux[0]="Debian"
linux[1]="RedHat"
linux[2]="Ubuntb"
linux[3]="Suse"

#输出第4个元素
echo "the fourth elemennt is ${linux[3]}"
#输出第4个元素的长度
echo "the length of fourth element is ${#linux[3]}"
#输出第一个元素的值
echo "the first element is ${linux}"
#输出第1和元素的长度
echo "the length of first element is ${#linux}"

7.3.4 通过循环遍历数组元素

vim ex7-16.sh

#! /bin/bash
#定义数组
array=(Mon Tue Wed Thu Fir Sat Sun)
#通过下标访问数组
for i in {0..6}
do
    echo "${array[$i]}"
done

#用参数获取数组长度

vim ex7-17.sh

#! /bin/bash
array=(Mon Tue Wed Thu Fir Sat Sun)
#获取数组长度
len="${#array[*]}"
len="${#array[@]}"
#通过循环结构遍历数组
for ((i=1;i<$len;i++))
do
    echo "${array[$i]}"
done

7.3.5 引用所有的数组元素

vim ex7-18.sh

#! /bin/bash

array=(Mon Tue Wed Thu Fir Sat Sun)
#通过循环输出所有的数组元素
for e in "${array[@]}"
do
    echo "${e}"
done

7.3.6 以切片方式获取部分数组元素

vim ex7-19.sh

#! /bin/bash

linux=("Debian","RedHat","Ubuntb","Suse","Fedora","UTS","CentOS")
#数组切片
echo "${linux[@]:2:4}"
echo ${linux[@]:2:4}
#将切片结果赋给一个变量
var=${linux[@]:2:4}
echo "${var}"

7.3.7 数组元素的替换

vim ex7-23.sh

#! /bin/bash

#定义数组
a=(1 2 3 4 5)
#输出替换结果
echo "the result is ${a[@]/3/100}"
#输出原始数组
echo "the old array is ${a[@]}"
#替换结果赋给一个变量
a=(${a[@]/3/100})# 数组需要用圆括号
#输出新的数组变量的值
echo "the new array is ${a[@]}"

20240901

day6

7.4删除数组

7.4.1 删除指定数组元素

vim ex7-24.sh

#! /bin/bash

linux=("Debian" "RedHat" "Ubuntb" "Suse" "Fedora" "UTS" "CentOS")
#输出原始数组长度
echo "the length of element is ${#linux[@]}"
#输出原始数组的值
echo "the old array is ${linux[@]}"
#删除下标为3的元素
unset linux[3]
#输出❤新数组长度
echo "the length of new array is ${#linux[@]}"
#输出新数组的值
echo "the new array is ${linux[@]}"

7.4.2 删除整个数组

vim ex7-25.sh

#! /bin/bash
linux=("Debian" "RedHat" "Ubuntb" "Suse" "Fedora" "UTS" "CentOS")
#删除整个数组
unset linux
echo "${linux[@]}"

7.5 数组的其他操作

7.5.1 复制数组

vim ex7-26.sh

#! /bin/bash
linux=("Debian" "RedHat" "Ubuntb" "Suse" "Fedora" "UTS" "CentOS")
#复制数组
linux2=("${linux[@]}")
echo "${linux2[@]}"

7.5.2 连接数组

vim ex7-27.sh

#! /bin/bash

#定义两个数组
linux=("Debian" "RedHat" "Ubuntb" "Suse" "Fedora" "UTS" "CentOS")
shell=("bash" "csh" "ksh" "rsh" "sh" "rc" "tcsh")
#连接数组
linuxshell=("${linux[@]}" "${shell[@]}")
#输出数组
echo "the new array is ${linuxshell[@]}"
echo "the length of linuxshell is ${#linuxshell[@]}"

7.5.3 加载文件内容到数组

vim ex7-28.sh

#! /bin/bash

#加载文件内容

content=(`cat "demo.txt"`)
#输出循环输出数组内容
for s in "${content[@]}"
do
    echo "$s"
done

20240901

day7

第8章 正则表达式

8.1 什么时正则表达式

8.1.1 为什么使用正则表达式

#常见支持正则表达式的UNIX工具如下
#grep命令族:用于匹配文本行
#sed流编辑器:用于改变输入流
#awk:用于处理字符串语言
#more或者less等:文件查看程序
#ed、vi或者vim等:文本编辑器

8.1.2 如何学习正则表达式

#1、重点在于理解元字符
#2、掌握好正则表达式的语法
#3、开拓思路,寻找最佳的表达方法

8.1.3 如何实践正则表达式

vim version.txt
app= 4.8.5
rev= 55324

vim ex8-1.sh

#! /bin/bash
str=`cat version.txt|grep rev`
echo "$str"

8.2 正则表达式基础

8.2.1 正则表达式的原理

8.2.2 基本正则表达式

1.行首定位符 “^”

vim ex8-2.sh

#! /bin/bash
#列出/etc目录中的以字母po开头的文件
str=`ls /etc | grep "^po"`
echo "$str"

2.行尾定位符"$"

vim ex8-3.sh

#! /bin/bash
#列出/etc目录中以conf结尾的文件名
str=`ls /etc | grep "conf$"`
echo "$str"

########^$:匹配所有空行#######

3.单个字符串匹配"."

####. 用来匹配任意单个字符,包括空格,但不包括换行符 \n ####
vim ex8-4.sh

#! /bin/bash
#列出所有包含字符串"samba"的文件名
str=`ls /etc | grep "samba"`
echo "$str"
echo "================"
#列出包含字符串samba以及另外一个字符的文件名
str=`ls /etc | grep "samba."`
echo "$str"

4. 限定符"*"

*表示匹配其前导字符的任意次数,不包括0次

vim ex8-5.sh

#! /bin/bash

#筛选出以字符s开头紧跟1个字符s,任意个字符s的文件名
srt=`ls /etc | grep "^ss*"`
echo "$str"
srt2=`ls /etc | grep "^sss*"`
echo "$str2"

5. 字符集匹配"[]"

vim ex8-6.sh

#! /bin/bash
#筛选所以以字符r开头并且紧跟着1个字符c的文本
str=`ls /etc |grep "^rc"`
echo "$str"
echo "==================="
#筛选所有以字符r开头紧跟一个字符c,下面一个字符为单个数字的文本行
str2=`ls /etc | grep "^rc[0-9]"`
echo "$str2"

6. 字符集不匹配"[^]"

[^]表示不匹配其中列出的任意字段

8.2.3 扩展正则表达式

1.限定符 “+”

+限定前面的字符至少出现一次

vim ex8-7.sh

#! /bin/bash
#筛选以字符串ss开头 后面至少紧跟着1个字符串 s 的文本行
str=`ls /etc | egrep "^sss+"`
echo "$str"

2.限定符 “?”

?限定前面的字符最多出现1次,即可以重复0或1次

3.竖线 “|” 和圆括号 “()”

| 或
() 一组可选值

vim ex8-9.sh

#! /bin/bash
#筛选含有字符串"ssh" "ssl"  或者以字符串 "yum"开头的文件行
str=`ls /etc | egrep "(ssh|ssl|^yum)"`

echo "$str"

8.2.4 Perl正则表达式

1.数字匹配\d

\d 等价于 [0-9]

vim ex8-10.sh

#! /bin/bash
#筛选所以以字符rc开头并且紧跟着1个数字的文本
str=`ls /etc |grep "^rc[0-9]"`
echo "$str"
echo "==================="
#筛选所以以字符rc开头并且紧跟着1个数字的文本
str2=`ls /etc | grep "^rc\d"`
echo "$str2"
echo "==================="
#筛选所以以字符rc开头并且紧跟着1个数字的文本
str3=`ls /etc | grep -P "^rc\d"`
echo "$str3"

2.非数字匹配\D

\D等价于[^0-9]

3.空白字符匹配\s

\s表示匹配任何空白字符,包括空格、制表符以及换页符;等价于 [\f\n\r\t\v]

4.非空白字符匹配\S

等价于 [^\f\n\r\t\v]

8.2.5 正则表达式字符集

8.3 正则表达式的应用

8.3.1 匹配单个字符

1.单个一般字符

vim ex8-11.sh

#! /bin/bash
#搜索含有字符串a的文本行
str=`grep "a" demo.txt`
echo "$str"

2.转义后的元字符

匹配? ?
匹配* *
匹配\ \

3.圆点表达式

匹配. .

4.方括号表达式

匹配? [?]
匹配* [*]
匹配\ []
匹配. [.]

8.3.2 匹配多个字符

vim demo4.txt
Inter:800-820-1100
Abit:800-820-0323
Asus:800-820-6655
Sony:800-810-2228
HP:8008100716
IBM:800-810-1818

vim ex8-16.sh

#!/bin/bash
#筛选 800-xxx-xxxx的电话
str=`egrep "800-[[:digit:]]{3}-[[:digit:]]{4}$"` demo4.txt
echo "$str"

20240902

day 8

8.3.3 匹配字符串的开头或者结尾

vim demo5.txt
020-85222213
86754234
800-820-1100
8008100716
abc123
98-3876

vim ex8-17.sh

#! /bin/bash

#筛选以3个数字开头的文本
str=`egrep "^[[:digit:]]{3}" demo5.txt`
echo "$str"

8.3.4 运算符优先级

8.3.5 子表达式

通过子表达式定位2个连续的HTML空格符

vim ex8-19.sh

#! /bin/bash

str=egrep "&nbsp;{2}" html.txt
echo “$str”

str1=egrep "(&nbsp;){2}" html.txt
echo “str1”

vim ip.txt
202.116.3.2
3.4.2
10.0.0.1
255.255.255.255.0
256.45.2.1

vim ex8-20.sh

#! /bin/bash

#匹配正确的ip地址
str=`egrep "^([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$" ip.txt`
echo "$str"
echo "========================"

0~255 [0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]

0~99 [0-9]{1,2}

100~199 1[0-9]{2}

200~249 2[0-4][0-9]

250~255 25[0-5]

str1=egrep "^([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" ip.txt
echo “$str1”

8.3.6 通配符

查找ex 开头的文件

ls -l ex*
l ex*

查找字符 d或者 l开头的文件

ls -l [dl]*

查找ex8-开头 然后编号是1-9的 .sh文件

ls -l ex8-[1-9].sh

8.4 grep 命令

8.4.1 grep命令基本语法

输出 demo3.txt文件含有simple的行内容

grep simple demo3.txt

输出 demo3.txt文件含有simple的行号

grep -c simple demo3.txt

8.4.2 grep命令族简介

grep
fgrep
egrep

20240903

day9

第9章 基本文本处理

9.1 使用echo 命令输出文本

9.1.1 显示普通字符串

vim ex9-1.sh

#! bin/bash
echo -n "what is your first name?"
read first
echo -n "what is your last name?"
read last
echo -n "what is your middle name?"
read midle
echo -n "what is your birthday?"
read birthday


echo "Hello World"
echo Hello World

9.1.2 显示转义字符

vim ex9-2.sh

#! /bin/bash

#退格符
echo -e "this is a\b string."
#禁止继续输出后面的文本
echo -e "hello \c world."
echo "====="
#换行符
echo -e "hello \n world."
#使用制表符输出格式
echo -e "Alice\t99"
echo -e "John\t82"
echo -e "Tom\t91"

9.1.3 显示变量

vim ex9-3.sh

#! /bin/bash

echo -n "Please input a name:"
read name
#输出变量的值
echo "Hell0,$name"

v1="sing"
v2="danc"
#错误的输出变量值的方法
echo "We are $v1ing,we are $2ing."
echo "*****"
echo "We are ${v1}ing,we are ${v2}ing."

20240904

day10

9.1.4 换行和不换行

默认情况下echo命令会自动追加换行符

使用转义字符 用 echo -e

换行使用 echo -n

vim ex9-5.sh

#! /bin/bash

echo "You are always in control of your search setting."
echo " Here's a quick review of the option that you can set."
echo "\n"
echo "换行"
echo -n "You are always in control of your search setting."
echo " Here's a quick review of the option that you can set."
echo "-e"
echo -e "You are always in control of your search setting.\c"
echo " Here's a quick review of the option that you can set."

9.1.5 显示命令执行结果

vim ex9-8.sh

#! /bin/bash

#显示date命令的执行结果
echo `date`
#显示ls命令的执行结果
echo `ls`

9.1.6 echo 执行结果的重定向

> 覆盖 >> 追加

vim ex9-9.sh

#! /bin/bash
#将要输出的结果写入文件
echo "Hello,World~Nice to meeet you!" > hello.txt
#将输出信息追加到结尾
echo "what is your name~" >> hello.txt

9.2 文本的格式化输出

9.2.1 使用UNIX制表符

制表符 \t

#使用echo输出99乘法表

vim ex9-10.sh

#! /bin/bash

#双层嵌套循环输出99乘法表
for ((i=1;i<10;i++))
do
    for ((j=1;j<=$i;j++))
    do
        #使用制表符列对齐
        echo -n -e "$i*$j\t"
    done
    #换行
    echo ""
done

vim ex9-11.sh

#! /bin/bash

#双层嵌套循环输出99乘法表
for ((i=1;i<10;i++))
do
    for ((j=1;j<=$i;j++))
    do
        #使用制表符列对齐
        let "k=i*j"
        echo -n -e "$i*$j=$k \t"
    done
    #换行
    echo ""
done

9.2.2 使用folf命令格式化行

语法: fold [options] [file…]
options的选项
-b:按字节计算宽度
-s:在空格处折断
-w:指定宽度,默认值是80列

9.2.3 使用fmt命令格式化段落

fmt [-width] [option] … [file] …

9.2.4 使用rev命令反转字符顺序

rev [file…]

vim demo3.txt

1 2 3 4 5 6 7 8 9 0
hello, world.

vim ex9-15.sh

#! /bin/bash

#反转文本行
str=`rev demo3.txt`
echo "$str"

20240905

day 11

9.2.5 使用pr命令格式化文本页

将文本文件内容转换成适合打印的内容
pr [option]…[file]…

将文本文件内容分成4栏输出

vim ex9-16.sh

#! /bin/bash

#格式化文本页
str=`pr -4 demo4.txt`
echo "$str"
#不输出标题
str2=`pr -t -4 demo4.txt`
echo "$str2"

9.3 使用sort命令对文本进行排序

9.3.1 sort 命令的基本用法

语法:sort [option] … [file] …

vim demo6.txt

Toy_Story HK 239 2972
The_Hill KL 63 2972
Star_Wars HK 301 4102
Boys_in_company_C HK 192 2192
Alien HK 119 1982
Aliens HK 532 4892
A_Few_Good_Men HK 445 5851

#对文件demo6.txt进行排序

vim ex9-18.txt

#! /bin/bash

#使用默认选项对文件进行排序
result=`sort demo6.txt`
#将结果保存到文件
echo "$result" > sorted_default.txt
#显示排序结果
cat sorted_default.txt

9.3.2 使用单个关键字排序

语法:-k pos1[,pos2]

sort命令中,一个文本行最多10列

vim ex9-19.sh

#!/bin/bash

#通过第2、3列来排序
result=`sort -k 2,3 demo6.txt`
echo "$result"

复制 demo6.txt 到 demo7.txt

cp demo6.txt demo7.txt

vim ex9-20.sh

指定列排序

#!/bin/bash
if [ $1 -gt 4 ]
then
    echo "column no.could not be greater than 4."
    exit
fi
#仅指定起始列
result=`sort -r -k $1 demo7.txt`
echo "$result"

#将某一列的关键字作为排序
语法:-k pos1[.start][,pos2.[end]]

#根据第14列的14~15个字符排序并输出到文件
sort -t ‘’ -n -k 4.14,4.15 demo7.txt > sorted_log
-t ‘’ 指定分隔符为空格
-n 将关键字作为数值来排序

9.3.3 根据指定的列来排序

根据输入的列号来对文件进行排序

vim ex9-22.sh

#! /bin/bash

if [ $1 -gt 4 ]
then
    echo "column no.could not be greater than 4."
    exit
else
    result=`sort -k $1,$1 demo7.txt`
    echo "$result"
    echo "compare___next___one_______"
    result2=`sort $1 demo7.txt`
    echo "$result2"
fi

9.3.4 根据关键字降序排序

语法:sort -r

vim ex9-23.sh

#! /bin/bash
#使用-r降序排序
result1=`sort -k 2,3 demo7.txt`
echo "$result1"
echo "compare___next___one___"
echo -e "\n"
result2=`sort -r -k 2,3 demo6.txt`
echo "$result2"
echo -e "\n"

echo "使用修饰符实现降序"
result3=`sort -k 2,3r demo6.txt`
echo "$result3"

9.3.5 数值列的排序

语法:-n

vim ex9-25.sh

#!/bin/bash
echo "对3列按数值排序"
result1=`sort -n -k 3,3 demo6.txt`
echo "$result1"
echo -e "compare1___next___one___ \n"
result2=`sort -k 3,3 demo6.txt`
echo "$result2"
echo -e "compare2___next___one___ \n"
result3=`sort -k 3,3n demo6.txt`
echo "$result3"
echo -e "compare3___next___one___ \n"
result4=`sort -k 3,3nr demo6.txt`
echo "$result4"

9.3.6 自定义列分隔符

cat /etc/passwd

复制做实验

cp /etc/passwd demo8.txt

vim ex9-26.sh

#! /bin/bash

echo -e "自定义分隔符 \n"
result=`sort -t : -k3n,3 demo8.txt`
echo "$result"
echo -e "\n"
result1=`sort -t "   " -k3n,3 demo8.txt`
echo "$result1"

9.3.7 删除重复行

语法:sort -u [file]

9.3.8 根据多个关键字排序

vim ex9-27.sh

echo -e "演示多个关键字排序方法\n"
echo -e "根据第3列的数字降序,第4列的数值升序\n"
result=`sort -k 3,3nr -k 4,4n demo7.txt`
echo "$result"

使用sort命令合并文件

语法:sort file1 file2

vim ex9-28.sh

#! /bin/bash
echo -e "合并文件并输出磁盘文件\n"
result=`sort demo6.txt demo6.txt > result_bind.txt`
cat result_bind.txt
echo -e "合并文件并删除重复行\n"
result1=`sort -u demo6.txt demo7.txt > result_bind_unique.txt`
cat result_bind_unique.txt
echo -e "合并后文件不排序\n"
result1=`sort -u -m demo6.txt demo7.txt > result_bind_unique_v2.txt`
cat result_bind_unique_v2.txt

20240906

day12

9.4 文本的统计

9.4.1 输出含有行号的文本行

cat -n ex9-1.sh

cat ex9-1.sh

grep -n “” ex9-1.sh

vim ex9-29.sh

#! /bin/bash

echo -e "为文本添加行号,并输出到文件\n"
nl -b a ex9-1.sh > textwithlineno.txt

cat textwithlineno.txt

echo -e "type2 \n"

nl ex9-1.sh > textwithlineno2.txt

cat textwithlineno2.txt

9.4.2 统计行数

演示用grep命令统计记录数

vim ex9-30.sh

#! /bin/bash
echo -n "Please input a name:"
#读取用户输入数据
read name
while [ $name != "e" ]
do
    #统计含有用户输入数据的行数
    quantity=`grep -c "$name" demo5.txt`
    echo "$quantity records contains $name"
    echo -n "Please input as name:"
    read name
done

语法:wc [option]…[file]…
option的常用选项
-c:统计文本的字节数
-m:统计字符数
-l:统计行数
-L:统计最长行的长度
-w:统计单词数

统计某个文件的行数

wc -l ex9-1.sh
9 ex9-1.sh

vim ex9-31.sh

#! /bin/bash
echo -e "统计文本行数\n"
lines=`cat ex9-1.sh | wc -l`
echo "the file has $lines lines"

vim ex9-32.sh

#! /bin/bash
echo -e "统计/etc目录下面有多少个以conf为扩展名的文件 \n"
count=`find /etc -name "*.conf" | wc -l`
echo "$count file have been found"

9.4.3 统计单词数和字符数

vim ex9-33.sh

#! /bin/bash
echo -e "统计单词数,只取数量"
words=`cat demo.txt | wc -w`
echo -e "there has $words words in file demo.txt \n"
echo -e "统计字符数 \n"
chars=`cat demo.txt | wc -m`
echo -e "there are $chars characters in file demo.txt \n"

9.5 使用cut命令选取文本列

9.5.1 cut命令及其语法

语法:cut option…[file]…
options的选项
-b:只选择指定的字节
-c:只选择指定的字符
-d:自定义列分隔符,默认值为制表符
-f:只选择列表中指定的文本列,文本列用列号表示,多个列之间用逗号隔开
-n:取消分隔多字节字符
-s:不输出不包含列分隔符的行

9.5.2 选择指定的文本列

cat /etc/passwd

vim ex9-34.sh

#! /bin/bash
echo -e "自定义分隔符为冒号,选择1和6列 \n"
result=`cut -d ":" -f 1,6 /etc/passwd`
echo "$result"

echo -e "type2 \n"

result2=`cut -d ":" -f 1-3 /etc/passwd`
echo "$result2"
echo -e "========================\n ================ \n"

result3=`cut -d ":" -f 3- /etc/passwd`
echo "$result3"

echo -e "*************** \n &&&&&&&&&&&&\n ^^^^^^^^^^ \n"

result4=`cut -d ":" -f 1,2,4-5 /etc/passwd`
echo "$result4"

9.5.3 选择指定数量的字符

vim ex9-35.sh

#! /bin/bash
echo -e "选择每行1~3个字符和第5个字符 \n"
result=`cut -c 1-3,5 /etc/passwd`
echo "$result"

20240908

day14

9.5.4 排除不包含列分隔符的行

cp demo8.txt demo8_pass.txt
在demo8_pass.txt末尾追加一条记录,user

vim ex9-36.sh

#! /bin/bash

echo -e "分别使用不含-s的选项cut命令和含有-s的cut命令来提取 demo8_pass.txt 文件的第一列\n"
echo -e "提取所有行的第一列\n"
cut -d ":" -f 1 demo8_pass.txt > allusers.txt
echo "all users:"
#显示所有行
cat allusers.txt
echo -e "只提取正确的行\n"

cut -s -d ":" -f 1 demo8_pass.txt > validusers.txt
echo "valid users:"
cat validusers.txt

9.6 使用paste命令拼接文本列

9.6.1 paste命令及其语法

语法:paste [option]…[file]…

vim students.txt

200200110 Abdul
200200164 Abram
200200167 Bartley
200200168 Bennert
200200172 Cecll
200200173 John
200200187 Cat

vim phone.txt

200200110 13611499594
200200164 13682239867
200200167 13710153203
200200168 13622259071
200200172 13430324699
200200179 13640656767

vim ex9-37.sh

#! /bin/bash
echo -e "拼接2个文件,并输出到磁盘\n"
paste students.txt phone.txt > contactinfo.txt
cat contactinfo.txt

9.6.2 自定义列分隔符

vim ex9-38.sh

#! /bin/bash
echo -e "使用逗号作为paste命令拼接结果的列分隔符\n"
echo -e "自定义列分隔符\n"
paste -d "," students.txt phone.txt > contactinfo_v2.txt

cat contactinfo_v2.txt

9.6.3 拼接指定的文本列

vim ex9-39.sh

#!/bin/bash
echo -e "通过cut和重定向将students.txt的第一列和phone.txt的第2列,并且去掉其他列\n"
echo -e "选择 students.txt的第一列\n"
cut -d " " -f 1,1 students.txt > students.tmp
cat students.tmp
echo -e "选择phone.txt的第2列\n"
cut -d " " -f 2,2 phone.txt > phone.tmp
cat phone.tmp
echo -e "将生成的2个文件拼接"
paste students.tmp phone.tmp > contactinfo_v3.txt
echo -e "输出拼接结果\n"
cat contactinfo_v3.txt
echo -e "============================= \n"
echo -e "先将2个文件拼接\n"
paste students.txt phone.txt > contactinfo_v4.txt
cat contactinfo_v4.txt
cut -f1,3 contactinfo_v4.txt > contactinfo_v5.txt
cat contactinfo_v5.txt

9.7 使用join命令联接文本列

9.7.1 join 命令及其语法

语法:join [option]…file1 file2

vim ex9-41.sh

#! /bin/bash
#使用默认选项连接2个文件
echo -e "students的文件内容:\n"
cat students.txt
echo -e "phone的文件内容:\n"
cat phone.txt
echo -e "关联2个文件\n"
result=`join students.txt phone.txt > contactinfo_join1.txt`
cat contactinfo_join1.txt

9.7.2 指定联接关键字列

vim score.txt

1 200200110 78
1 200200164 78
2 200200167 92
3 200200168 87
4 200200172 65

vim ex9-42.sh

#! /bin/bash

echo -e "指定-2 和-1选项为关键字列\n"
echo -e "指定students的第一列和score的第2列作为关键字\n"
result=`join -1 1 -2 2 students.txt score.txt > students_score.txt`
cat students_score.txt

9.7.3 使用内连接文本文件

join的默认选项就是内连接

9.7.4 左连接文本文件

语法:join -a 1 filenum file1 file2

vim ex9-43.sh

#! /bin/bash
echo -e "通过join命令左连接文件\n"
result=`join -a 1 students.txt phone.txt > students_phone.txt`
cat students_phone.txt

9,7,5 右连接文本文件

语法:join -a 2 filennum file1 file2

vim ex9-44.sh

#!/bin/bash
echo -e "右连接文件\n"
result=`join -a 2 students.txt phone.txt > students_phone2.txt`
cat students_phone2.txt

9.7.6 全链接文本文件

语法:join -a 1 -a 2 filennum file1 file2

vim ex9-45.sh

#! /bin/bash
echo -e "全链接文本文件"
result=`join -a 1 -a 2 students.txt phone.txt > students_phone12.txt`
cat students_phone12.txt

9.7.7 自定义输出列

vim ex9-46.sh

#! /bin/bash
echo -e "连接文件,并指定输出列的清单\n"
echo -e "输出第一个文件的1,2列,第2个文件的第3列"
result=`join -1 1 -2 2 -o 1.1 1.2 2.3 students.txt score.txt > students_score_11223.txt`
cat students_score_11223.txt

20240909

day15

9.8 使用tr命令替换文件内容

9.8.1 tr命令及其语法

语法:tr [option] set1 [set2]

9.8.2 去吃重复出现的字符

vim demo9.txt
Windows RT is a new Windows-bases operating systemmmmmmmmmmmmmmmmmmmmmmmmmm that’s optimized for thin and bigggggggggggggggggggggggggggggght PCs that have extended battery life and are desugned fir life on the go.

vim ex9-47.sh

#! /bin/bash
echo -e "压缩重复字符\n"
result=`tr -s "[a-z]" < demo9.txt`

echo "$result"

9.8.3 删除空行

vim demo10.txt

hello,world!

i
love
linux.

and

you

?

vim ex9-48.sh

#! /bin/bash
echo -e "删除空白行\n"
result=`cat demo10.txt | tr -s ["\n"]`
echo -e "$result\n"

9.8.4 大小写切换

语法:tr [a-z] [A-Z]

vim ex9-49.sh

#! /bin/bash
echo -e "将当前目录所有文件的文件名转为大写\n"
for file in `ls ex8*`;do
    echo "$file" | tr 'a-z' 'A-Z'
done

9.8.5 删除指定字符

vim demo11.txt
Monday 09:00
Tuesday 09:10
Wednesday   10:11
Thursday    11:30
Firday  08:00
Saturday    07:40
Sunday  10:00

vim ex9-50.sh

#!/bin/bash
echo -e "删除数字和冒号\n"
result=`tr -d "[0-9][:]" < demo11.txt`
echo -e "$result\n"

20240910

day16

第10章 流编辑

10.1 sed命令及其语法

10.1.1 sed命令及其语法

sed [options] [script] [inputfile…]

10.1.2 sed命令的工作方式

sed [options] commands inputfile
sed [options] -f script inputfile

10.1.3 使用行号定位文本行

1、定位某个特定的行
x
2、定位某段连续的行
x,y
3、指定起始行和步长
first~step
4、指定文件的第一行和最后一行
第一行:1
最后一行:$
5、指定某行后面的几行
x,+n

10.1.4 使用正则表达式定位文本行

语法:/regexp/
*:表示前置表达式重复了0次或者多次
+:与星号类似,前置表达式出现1次以上
?:前置表达式重复出现0次或者1次
.:匹配任意字符
[]:匹配方括号中任意单个字符
[^]:匹配不出现在方括号中的任意单个字符
{i}:前置表达式最少出现i次
{i,j}:匹配前置表达式最少出现i次,最多出现j次

20240911

day17

10.2 sed命令的常用操作

10.2.1 sed编辑命令基本语法

语法:[address1[,address2]] command [argument]

10.2.2 选择文本

[address1[,address2]] p

vim ex10-1.sh

#! /bin/bash

echo -e "输出1~3行,不使用-n选项\n"
sed '1,3p' students.txt

echo -e "===================\n"

echo -e "输出1~3行,使用-n选项\n"
sed -n '1,3p' students.txt

vim ex10-2.sh

#! /bin/bash
echo -e "使用正则表达式定位\n"
result=`sed -n '/^20020017/ p' students.txt`

echo "$result"

echo -e "输出第一行\n"
sed -n '1 p' students.txt

echo -e "输出最后一行\n"
sed -n '$ p' students.txt

echo -e "输出奇数行\n"
sed -n '1~2 p' students.txt

echo -n "输出偶数行\n"
sed -n '0~2 p' students.txt

echo -e "使用正正则表达式和行号来表达\n"
sed -n '/Abdul/,5 p' students.txt

10.2.3 替换文本

语法:[address1[,address2]] s/pattern/replacement/[flag]

vim ex10-3.sh

#! /bin/bash
echo -e "使用s命令替换文本"

echo "substitute the first pattern"
echo -e "只将每行中第一次出现的小写字母e替换为大写字母E\n"
result=`sed 's/e/E/' students.txt`
echo "$result"
echo -e "substitute all the patterns.\n"
echo -e "将每一处的小写字母e替换为大写字母E\n"
result=`sed 's/e/E/g' students.txt`
echo "$result"

vim ex10-4.sh

#! /bin/bash
echo -e "替换1~3行所有小写字母e替换为E\n"
result=`sed '1,3 s/e/E/g' students.txt`
echo "$result"

vim ex10-5.sh
#! /bin/bash
echo -e "使用混合位置参数\n"
result=`sed '1,/^200200167/ s/e/E/g' students.txt`
echo "$result"

vim HTML.txt
<div class="xspace-itemdata">
<a herf="#xspace-tracks">view(1881)</a>
<a herf="xspace-itemreply">review(5)</a>
<a herf="#xspace-itemform">score</a>
</div>

vim ex10-6.sh
#! /bin/bash
echo -e "将文件中的HTML标记替换为空\n"
result=`sed 's/<[^>]*>//g' HTML.txt`
echo "$result"

20240912

day 18

vim demo1.txt
this is a string.

vim ex10-7.sh

#! /bin/bash
echo -e "引用与模式相匹配的子串\n"
result=`sed 's/string/long &/' demo1.txt`
echo "$result"

vim ex10-8.sh

#!/bin/bash
echo -e "通过数字来引用模式中的子串\n"
result=`sed 's/\(This\)\(is\)\(a\)\(string\)/\2\1\3\4/' demo1.txt`
echo "$result"

10.2.4 删除文本

语法:[address1,[,address2]] d

vim ex10-9.sh
#!/bin/bash
echo -e "删除第1行\n"
result=`sed -e '1 d' students.txt`
echo -e "$result\n"

vim ex10-10.sh
#! /bin/bash
echo -e "删除最后一行\n"
result=`sed -e '$ d' students.txt`
echo -e "$result\n"

vim ex10-11.sh

#!/bin/bash
echo -e "实现更为复杂的定位方法\n\n"
echo -e "删除1~4行\n"
result=`sed -e '1,4 d' students.txt`
echo "$result\n"

echo "==============================="

echo -e "删除奇数行\n"
result=`sed -e '1~2 d' students.txt`
echo -e "$result\n"
echo "==============================="

echo -e "删除偶数行\n"
result=`sed -e '0~2 d' students.txt`
echo -e "$result\n"
echo "==============================="

echo -e "删除从第一行开始,一直到以200200172开头的行\n"
result=`sed -e '1,/^200200172/ d' students.txt`
echo -e "$result\n"
echo "+++++++++++++++++++++++++++++++"

echo -e "删除第4行开始到最后一行\n"
result=`sed -e '4,$ d' students.txt`
echo -e "$result\n"

echo -e "删除所有空白行\n"
sed '$ d' students.txt

10.2.5 追加文本行

语法:[address1] a string

vim ex10-12.sh
#!/bin/bash
cat students.txt
echo -e "+++++++++++++++++++++ \n"
echo -e "在第2行后面追加文本\n"
result=`sed '2 a 200200109  Tom' students.txt`
echo -e "$result\n"

echo -e "通过正则表达式的方式追加文本位置\n"
echo -e "在以200200110开头的文本行后面追加文本\n"
result=`sed '/^200200110/ a 200200109       Tom' students.txt`
echo -e "$result\n"

10.2.6 插入文本

语法:[address1] i string

vim ex10-14.sh

#!/bin/bash

echo -e "在以200200110开头的文本行前面插入文本\n"
result=`sed '2 i 200200109      Tom' students.txt`
echo -e "$result\n"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值