重庆网站推广招聘,关于网站开发的学校,wordpress源码站整站源码,网站开发需要配置哪些人员1.工作原理#xff1a; sed是一种流编辑器#xff0c;它是文本处理中非常有用的工具 能够完美的配合正则表达式使用#xff0c;处理时#xff0c;把当前处理的行存储在临时缓冲区中#xff0c;称为模式空间#xff0c;接着用sed命令处理缓冲区中的内容 处理完成后…1.工作原理 sed是一种流编辑器它是文本处理中非常有用的工具 能够完美的配合正则表达式使用处理时把当前处理的行存储在临时缓冲区中称为模式空间接着用sed命令处理缓冲区中的内容 处理完成后把缓冲区的内容送往屏幕。接着处理下一行这样不断重复直到文件末尾。文件内容并没有改变 sed的特点 sed命令是将一系列的编辑命令应用于一批文本的理想工具 sed命令是一个非交互式的文本编辑器它可以对来自文本文件以及标准输入的文本进行编辑。其中标准输入可以是来自键盘、文件重定向、字符串、变量或者是管道的文本 sed命令会从文件或者标准输入中一次读取一行数据将其复制到缓冲区最多8192字节然后读取命令行或者脚本的编辑子命令对缓冲区中的文本行进行编辑。重复此过程一直到所有的文本行都处理完毕 2.sed基本语法 语法 sed OPTIONS… [SCRIPT] [INPUTFILE…] 参数 常用的选项说明-n--quiet--silent只打印匹配的行-i直接编辑原文件而不是由屏幕输出默认不对原文件进行操作-e直接在命令行模式上进行sed的动作编辑不会对原文件修改-r使用扩展正则表达式-f直接将sed的动作写在一个文件内-f filename则可以执行filename内的sed动作[rootquruixiang ~]# sed -n 3p /etc/passwd 3.模式空间中的编辑操作 地址定界查询 # 1##为数字指定要进行处理操作的行1表示第一行
[rootquruixiang ~]# sed -n 3,4p /etc/passwd
# 2$表示最后一行多个文件进行操作的时候为最后一个文件的最后一行
[rootquruixiang shellstudy]# sed -n $p test8.sh
# 3/regexp/表示能够被regexp匹配到的行
[rootquruixiang ~]# sed -n /test/p /etc/passwd
## regexp即基于正则表达式的匹配
# 4/regexp/I匹配时忽略大小写
[rootquruixiang shellstudy]# sed -n /test/Ip /etc/passwd
# 5\%regexp%: 任何能够被regexp匹配到的行换用%用其他字符也可以如#为边界符号
[rootquruixiang shellstudy]# sed -n \%echo%p test8.sh
# 6addr1,addr2指定范围内的所有的行范围选定
[rootquruixiang ~]# sed -n 3,6p /etc/passwd
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
# 常用地址定界表示方式
## a0/regexp/从起始行开始到第一次能够被regexp匹配到的行。
[rootquruixiang shellstudy]# sed -n 0,/bin/p test8.sh
## b/regexp/,/regexp/被模式匹配到的行内的所有的行。
## 7first~step指定起始的位置及步长例如1~2表示1,3,5…
[rootquruixiang shellstudy]# sed -n 1~3p test8.sh
#!/bin/bash
#Version:v1.0
#Description:
func()echo 1fi
echo $result ## 8addr1,N指定行以及以后的N行
[rootquruixiang ~]# sed -n 3,4p /etc/passwd
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
### addr1,~Naddr1开始的行N结束的行
[rootquruixiang ~]# sed -n 3,~4p /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin 注意事项: 如果没有指定地址表示命令将应用于每一行 如果只有一个地址表示命令将应用于这个地址匹配的所有行 如果指定了由逗号分隔的两个地址表示命令应用于匹配第一个地址和第二地址之间的行(包括这两行) 如果地址后面跟有感叹号表示命令将应用于不匹配该地址的所有行 常用编辑命令编辑 # 1d删除匹配到的行
[rootquruixiang shellstudy]# sed 3d test8.sh
[rootquruixiang shellstudy]# sed /echo/d test8.sh
[rootquruixiang shellstudy]# sed /^$/d test8.sh # 删除空行
# 2p打印当前模式空间内容
[rootquruixiang shellstudy]# sed /echo/p test8.sh
# 3a \textappend,表示在匹配到的行之后追加内容
[rootquruixiang shellstudy]# sed $a\system test8.sh
[rootquruixiang shellstudy]# sed 3a\systemctl test8.sh
[rootquruixiang shellstudy]# sed /echo/a\systemctl test8.sh
# 4i \textinsert,表示在匹配到的行之前追加内容
[rootquruixiang shellstudy]# sed 3i\sys test8.sh
[rootquruixiang shellstudy]# sed /echo/i\sys test8.sh
# 5c \textchange,表示把匹配到的行和给定的文本进行交换
[rootquruixiang shellstudy]# sed /echo/c\system test8.sh
# 6s/regexp/replacement/flages查找替换,替换regexp匹配到的内容其中/可以用其他字符代替
sed -i /^SELINUX/ s/SELINUX.*/SELINUXdisabled/ /etc/selinux/config
[rootquruixiang shellstudy]# sed s/fun/##/1 test8.sh # 替换每行中第一个匹配的fun替换为#### 例如## 其他编辑命令## 常用的flages## g全局替换默认只替换第一[rootquruixiang shellstudy]# sed s/echo/system/g test8.sh[rootquruixiang shellstudy]# sed s/^/##/g test8.sh # 文件中的每一行开头加一个##[rootquruixiang shell]# sed -i s/^#[[:space:]]*//g test.txt # 删除开头#和至少一个空字符的行 ## i 不区分大小写## p如果成功替换则打印[rootquruixiang shellstudy]# sed s/echo/system/p test8.sh# 7r 读入文件内容追加到匹配行后面
[rootquruixiang shellstudy]# sed r 2.txt 1.txt # 在1.txt中的每一行后都写入2.txt的内容
[rootquruixiang shellstudy]# sed 3r 2.txt 1.txt # 在1.txt中的第3行后写入2.txt的内容
[rootquruixiang shellstudy]# sed /245/r 2.txt 1.txt # 在1.txt中的匹配行后写入2.txt的内容
# 8R 读入文件一行内容追加到匹配行后面
# 9y y/source/dest/ 固定长度替换要求替换的字符串长度相等
# 10w /path/to/somefile将匹配到的文件内容追加到指定的文件末尾
[rootquruixiang shell]# sed -n w test.txt passwd # 在test.txt的末尾0后写入passwd的内容 4.sed扩展
特殊符号说明对指定行以外的所有行应用命令打印当前行行号~“first~step”表示从first行开始以步长step递增代表被替换的内容;实现一行命令语句可以执行多条sed命令{}对单个地址或地址范围执行批量操作地址范围中用到的符号做加法运算
# 打印匹配到echo的行号
[rootquruixiang shellstudy]# sed -n /echo/ test8.sh
# 打印最后一行的行号
[rootquruixiang shellstudy]# sed -n $ test8.sh
# 删除3之外的所有行
[rootquruixiang shellstudy]# sed 3!d test8.sh
# 删除1~3之外的所有行
[rootquruixiang shellstudy]# sed 1,3!d test8.sh
# 删除匹配到fi到最后一行之外的所有行
[rootquruixiang shellstudy]# sed /fi/,$!d test8.sh
# 删除从匹配fi的行到最后一行
[rootquruixiang shellstudy]# sed /fi/,1d test8.sh
# 删除匹配echo或fi的行
[rootquruixiang shellstudy]# sed /echo\|fi/d test8.sh
# 删除1~3行中匹配内容bin的行
[rootquruixiang shellstudy]# sed 1,3{/bin/d} test8.sh
# 打印匹配echo的行的行号和内容
[rootquruixiang shellstudy]# sed -n /echo/{;p} test8.sh
# 打印3行到10行的内容
[rootquruixiang shell]# sed -n 3,10{;p} test.txt