shell-sed命令

2019-08-16  本文已影响0人  哆来咪发都不会

sed主要用于替换指定的字符

语法
用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...

  -n, --quiet, --silent
                 取消自动打印模式空间
  -e 脚本, --expression=脚本
                 添加“脚本”到程序的运行列表
  -f 脚本文件, --file=脚本文件
                 添加“脚本文件”到程序的运行列表
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 指定“l”命令的换行期望长度
  --posix
                 关闭所有 GNU 扩展
  -r, --regexp-extended
                 在脚本中使用扩展正则表达式
  -s, --separate
                 将输入文件视为各个独立的文件而不是一个长的连续输入
  -u, --unbuffered
                 从输入文件读取最少的数据,更频繁的刷新输出
      --help     打印帮助并退出
      --version  输出版本信息并退出

按条件过滤
[root@localhost wang]# cat test.txt 
#abc
#abcd
#abcdef
#123
1234
12345
def
abcdef
abc123
def123abc
ABC
#ABCD

#打印包含abc的行,分隔符可以用/,也可以用@,其他符号也行
[root@localhost wang]# sed -n '/abc/'p test.txt 
#abc
#abcd
#abcdef
abcdef
abc123
def123abc
[root@localhost wang]# 

sed不想用转义符号时,需要加-r参数

#打印1开头的行
[root@localhost wang]# sed -n '/^1/'p test.txt 
1234
12345

#打印abc结尾的行
[root@localhost wang]# sed -n '/abc$/'p test.txt 
#abc
def123abc
 
#打印第2行
[root@localhost wang]# sed -n '2'p test.txt 
#abcd

#打印2-7行
[root@localhost wang]# sed -n '2,7'p test.txt 
#abcd
#abcdef
#123
1234

#打印第5行和后面的所有行
[root@localhost wang]# sed -n '5,$'p test.txt 
1234
12345
def
abcdef
abc123
def123abc
ABC
#ABCD

#不区分大小写
[root@localhost wang]# sed -n '/abc/'Ip test.txt 
#abc
#abcd
#abcdef
abcdef
abc123
def123abc
ABC
#ABCD
[root@localhost wang]# 

删除满足条件的行
#不显示前5行,但文件中未删除
[root@localhost wang]# sed '1,5'd test.txt 

1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD
[root@localhost wang]# cat test.txt 
#abc
#abcd
#abcdef
#123


1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD


#文件中前5行删除
[root@localhost wang]# sed -i '1,5'd test.txt 
[root@localhost wang]# cat test.txt 

1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD
[root@localhost wang]# 
替换
#全局替换1-10行的abc为ABC,g为全局替换
[root@localhost wang]# sed  '1,10s/abc/ABC/g' test.txt 
#全局替换所有行的abc为ABC
[root@localhost wang]# sed  's/abc/ABC/g' test.txt 

#将第一列和最后一列进行替换
[root@localhost wang]# cat 123.txt 
abc:jsak:12njsdjq:kljlkhsa:hahah:wang
wanghuan:kjskjfd:popl9:q2j2q:ruhr
#将abc和wang替换,将wanghuan和ruhr替换
[root@localhost wang]# sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' 123.txt 
wang:jsak:12njsdjq:kljlkhsa:hahah:abc
ruhr:kjskjfd:popl9:q2j2q:wanghuan
[root@localhost wang]# 
# 分成3段:
([^:]+):
(.*):        #贪婪匹配,直到最后一个冒号
([^:]+)

上一篇 下一篇

猜你喜欢

热点阅读