1、sed
SED(1) User Commands SED(1)
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed可删除(delete)、改变(change)、添加(append)、插入(insert)、合并、交换文件中的资料行,或读入其它档的资料到文件中,也可替换(substuite)它们其中的字串、或转换(tranfer)其中的字母等等。例如将文件中的连续空白行删成一行、"local"字串替换成"remote"、"t"字母转换成"T"、将第10行资料与第11资料合并等.
当sed由标准输入读入一行资料并放入pattern space时,sed依照sed script 的编辑指令逐一对pattern space内的资料执行编辑,之後,再由pattern space内的结果送到标准输出,接着再将下一行资料读入,如此重复执行上述动作,直至读完所有资料行为止.
记住:
(1)sed总是以行对输入进行处理
(2)sed处理的不是原文件而是原文件的拷贝
命令行概述:
sed 编辑指令的格式如下 :
[address1[,address2]]function[argument]
其中 , 位址参数 address1 、address2 为行数或 regular expression 字串 , 表示所执行编辑的资料行; 函数参数 function[argument] 为 sed 的内定函数 , 表示执行的编辑动作。
1、删除
(1) sed -e '1d' file (删除第一行)
Loong:/home/yee/lottery# cat b.txt
08,12,13,26,29,33|01 2012-10-23
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
12,13,19,22,28,29|11 2012-10-16
01,07,08,20,23,24|11 2012-10-14
06,07,21,25,27,33|11 2012-10-21
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
06,07,21,25,27,33|11 2012-10-21
Loong:/home/yee/lottery# sed -e '1d' b.txt 删除第一行输出
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
12,13,19,22,28,29|11 2012-10-16
01,07,08,20,23,24|11 2012-10-14
06,07,21,25,27,33|11 2012-10-21
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
06,07,21,25,27,33|11 2012-10-21
Loong:/home/yee/lottery# cat b.txt 文本内容并没有改变,所以处理的是文件的拷贝
08,12,13,26,29,33|01 2012-10-23
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
12,13,19,22,28,29|11 2012-10-16
01,07,08,20,23,24|11 2012-10-14
06,07,21,25,27,33|11 2012-10-21
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
06,07,21,25,27,33|11 2012-10-21
Loong:/home/yee/lottery#
(2) sed -e '1,4d' file (删除第1到第4行)
Loong:/home/yee/lottery# sed -e '1,4d' b.txt
01,07,08,20,23,24|11 2012-10-14
06,07,21,25,27,33|11 2012-10-21
06,07,21,25,27,33|11 2012-10-21
08,10,16,25,28,33|09 2012-10-18
06,07,21,25,27,33|11 2012-10-21
Loong:/home/yee/lottery#
(3) sed -e '/#/d' file (删除含有'#'号的行)
:删除含有字母xx的行
sed -e '/xx/d' file
: 删除 除含有字符串xx的所有行
sed -e '/xx/ !d' file
Loong:/home/yee/lottery# cat pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '/-/d' pam.conf
# /etc/pam.conf #
#
# NOTE
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery#
Loong:/home/yee/lottery# sed -e '/etc/d' pam.conf
# ---------------------------------------------------------------------------#
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '/etc/!d' pam.conf
# /etc/pam.conf #
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
Loong:/home/yee/lottery#
(4) sed -e '/word1/, /word2/d' file (删除从含有单词word1到含有单词word2的行)
sed -e '10,/word1/d' file
删除文件中从第10行到含有word1的行
sed -e '/word1/,10 d' file
和上面的匹配相反,删除从含有word1的行到第10行
Loong:/home/yee/lottery# cat pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '/etc/,/etc/d' pam.conf
# ---------------------------------------------------------------------------#
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '6,/etc/d' pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '/etc/, 10 d' pam.conf
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery#
(5) sed -e '/t.*t/d' file (删除含有两个t的行) 支持正则表达式的写法
Loong:/home/yee/lottery# sed -e '/t.*t/d' pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# ---------------------------------------------------------------------------#
# Format:
# name type flag #
Loong:/home/yee/lottery#
2、 替换
Sed 可替换文件中的字串、资料行、甚至资料区。其中,表示替换字串的指令中的函数参数为s;表示替换资料行、或资料区>的指令中的函数参数为c。
*行的替换
(1) sed -e '1c/ ########test####### file (把第一行替换成#########test#######)
思考: 把第3行替换成########test#######
sed -e '3c/########test#######' file
Loong:/home/yee/lottery# cat pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '1c/########test#######' pam.conf
/########test#######
# /etc/pam.conf #
# ---------------------------------------------------------------------------#
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery# sed -e '3c/########test#######' pam.conf
# ---------------------------------------------------------------------------#
# /etc/pam.conf #
/########test#######
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery#
(2) sed -e '1,10c/I can do it' file (把1到10行替换成一行:########test#######)
思考: 换成两行(########test#######,########test#######)
sed -e '1,10c/########test#######\n########test#######' file
Loong:/home/yee/lottery# sed -e '1,3c ########test#######' pam.conf
########test#######
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
Loong:/home/yee/lottery#
Loong:/home/yee/lottery# sed -e '1,3c /########test#######\n########test#######' pam.conf
/########test#######
########test#######
#
# NOTE
# ----
#
# NOTE: Most program use a file under the /etc/pam.d/ directory to setup their
# PAM service modules. This file is used only if that directory does not exist.
# ---------------------------------------------------------------------------#
# Format:
# serv. module ctrl module [path] ...[args..] #
# name type flag #
*字符的替换
(3) sed -e 's/word1/& word2/' file (将每一行的word1单词替换成word2 ;s参数最多与两个位置参数相结合,函数参数s中有两个特殊的符号:
& : 代表pattern
/n : 代表 pattern 中被第 n 个 /( 、/) 所括起来的字串。例如
A、sed -e 's/w1/& w2/' file # w1的地方输出 w1 w2
Loong:/home/yee/shell# sed -e 's/passwd/&com/' du.txt
957 ./test_practise.sh
503 ./full-name-output.sh
1386 ./passwdcom 追加在后文
181 ./self_add2.sh
14 ./deal_passwdcom.sh
Loong:/home/yee/shell# sed -e 's/passwd/com/' du.txt
957 ./test_practise.sh
503 ./full-name-output.sh
1386 ./com 直接替换
181 ./self_add2.sh
14 ./deal_com.sh
Loong:/home/yee/shell#
B、 sed -e 's//(test/) /(my/) /(car/)/[/2 /3 /1]/' file #结果: [my car test]
3, 命令的复用
一次执行多个命令的方式有三种:
(1) sed 's/w1/& w2/g; 1/i/words' filename (使用;号把命令隔开,注意前面不加-e参数)
(2) sed -e 'cmd1' -e 'cmd2' filename (使用多个-e参数)
更多参考:
http://blog.csdn.net/zg_hover/article/details/1804481
本文来自博客园,作者:{Julius},转载请注明原文链接:https://www.cnblogs.com/bestechshare/p/16447851.html
可微信加我,了解更多,WeChat:{KingisOK}