sed的基础用法是怎么样的

发布时间:2021-11-08 16:23:29 作者:柒染
来源:亿速云 阅读:114

今天就跟大家聊聊有关sed的基础用法是怎么样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Sed包含多个功能,最常用的是替换命令

替换

s/pattern/replacement/flags

Flags包括如下

n1-512之间的数字,对第n次匹配进行替换

g:对所有匹配情况进行全局修改

p:打印模式所有内容

wfile,将模式内容写入文件file

                                                                                                                                         

对于replacement,如下字符有特殊含义

&:将其替换为pattern

\n:匹配第n个字串,在pattern中用”\(“”\)”指定

\:转义字符

 

&替换成pattern,可\&转义

[oracle@ ~]$ cat test

ORA

[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly \& Associates, Inc./g'

O’ Reilly & Associates, Inc.

[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly & Associates, Inc./g'

O’ Reilly ORA Associates, Inc.

 

[oracle@ ~]$ cat test1

See Section 19.40  See Section 18.89

See Section 19.09

See Section 07.10

 [oracle@ ~]$ sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/2' test1 –为每行的第二个匹配字符包围”()”

See Section 19.40  (See Section 18.89)

See Section 19.09

See Section 07.10

 

将每行第2See替代为换行字符

[oracle@ ~]$ sed 's/See/\\n/2' test1  --不起作用

See Section 19.40  \n Section 18.89

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed 's/See/\

> /2' test1  --成功换行,但不太好维护

See Section 19.40 

 Section 18.89

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed 's/See/\n/2' test1  --使用\n替代

See Section 19.40 

 Section 18.89

See Section 19.09

See Section 07.10

 

搜索每行第2个匹配模式,并将其第1/2个元素互换位置

[oracle@ ~]$ sed 's/\(See Section\) \([1-9][0-9]\.[1-9][0-9]\)/\2 \1/2' test1

See Section 19.40  18.89 See Section

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed '/0[1-9]\./ s/\(See Section\) \([0-9][0-9]*\.[1-9][0-9]\)/\2 \1/' test1 –先搜索包含”0[1-9].”的行,如果其满足匹配模式,则将其第1/2个元素互换位置

See Section 19.40  See Section 18.89

See Section 19.09

07.10 See Section

 

[oracle@ ~]$ cat test1 | grep UNIX

UNIX

[oracle@ ~]$ sed 's/UNIX/\\s-1&\\s0/' test1  --使用&替代前面的UNIX

\s-1UNIX\s0

 [oracle@ ~]$ sed 's/UNIX/\s-1\&\\s0/' test1 –使用\&将其作为普通字符,\\s改为\s,而s不是特殊字符所以\s等同于s

s-1&\s0

 [oracle@ ~]$ sed 's/UNIX/s-1\&\\s0/' test1

s-1&\s0

 

忽略大小写

[oracle@ ~]$ cat test5
who finger w last
[oracle@ ~]$ sed 's/FINger/www/gI' test5
who www w last

 

2 删除(d)

如果行匹配则删除该行

/^$/d –删除空行

[oracle@ ~]$ cat test3

adf

 

sdf

[oracle@ ~]$ sed '/^$/d' test3  --删除空行

adf

sdf

[oracle@ ~]$ sed '/./!d' test3 --删除空行,!表示not,/./!即不带有字符的行

adf

sdf

[oracle@ ~]$ sed '/./d' test3 – 删除非空行

 

[oracle@ ~]$ sed '$d' test3  --删除最后一行

adf

 

[oracle@ ~]$ sed '2d' test3 –删除第2

adf

sdf

[oracle@ ~]$ sed '$!d' test3 –只保留最后一行数据,其余全删除

Sdf

 

3 追加(a)/插入(i)/更改(c)

格式如下

[line-address][a|i|c] text

 

注:<<sed and awk>>(O’Reilly)指出 “这些命令必须在多行上指定, (\用于转义行尾)

[line-address][a|i|c]\

Text

但个人试验发现不需要这么麻烦

 

[oracle@ ~]$  cat test3

adf

 

sdf

在第1行和最后1行前后分别添加信息

[oracle@ ~]$ sed -e '1i insert before the first line' -e '$a append after the last line' test3

insert before the first line

adf

 

sdf

append after the last line

使用更改(c)命令有时比替换更高效,如下:

将所有以”.sp”开头的行的输入参数替换为5

[oracle@ ~]$ cat test4

.sp 1.5

.sp 4

.sp

5.sp 67

[oracle@ ~]$ sed '/^\.sp/c \.sp 5' test4  --搜索以.sp开头的行,将整行替代为.sp 5

.sp 5

.sp 5

.sp 5

5.sp 67

 

4 列表(l)

显示模式空间的内容,将非打印字符显示为两个数字的ASCII代码,类似vi(:l)或者cat -v

可用于检测不可见字符;

oracle@ ~]$ cat -v test5

who finger w last^M

^M

^M

[oracle@ ~]$ sed -e "l" test5

who finger w last\r$

who finger w last

\r$

 

\r$

 

[oracle@ ~]$ sed -n -e "l" test5

who finger w last\r$

\r$

\r$

其中-n选项作用如下:-n, --quiet, --silent suppress automatic printing of pattern space

 

 

5下一行(n)

输出模式空间的内容,然后读取输入的下一行,而不返回脚本的顶端;

[oracle@usuwsoadb06 ~]$ cat -n test7

     1  apple

     2  banana

     3  mango

     4  orange

     5  strawberry

[oracle@usuwsoadb06 ~]$ sed '/ban/{n;d;}' test7  --删除匹配模式的下一行

apple

banana

orange

strawberry

 

--Nn的区别,前者会输出当前匹配行

[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {n; p;}' test7

mango

[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {N; p;}' test7

banana

mango

 

寻找包含ban字符的下一行,将其替换为justin

[oracle@usuwsoadb06 ~]$ sed '/ban/ {n; s/.*/justin/;}' test7

apple

banana

justin  --原本的mango现在变为justin

orange

strawberry

 

 

6读写文件(r/w)

 

写文件

从某个文件读取符合条件的行,并将其写入另外一个文件,语法如下:

Sed ‘/pattern/w outputfile’ inputfile

 

[oracle@ ~]$ cat -n test7

     1  apple

     2  banana

     3  mango

     4  orange

     5  strawberry

 [oracle@ ~]$ sed -n '2,4w test8' test7 –test72-4行输入到test8

[oracle@ ~]$ cat test8

banana

mango

orange

[oracle@ ~]$ sed -n '/mango/,$w test8' test7 –test7从包含mango行直到最后一行的数据写入test8

[oracle@ ~]$ cat test8

mango

orange

strawberry

[oracle@ ~]$ sed -n '$!w test8' test7 –除去最后一行将test7所有内容写入test8

[oracle@ ~]$ cat test8

apple

banana

mango

orange

 

读文件 --并不会实际改变文件内容

Sed ‘/pattern/r outputfile’ inputfile

--将文件内容写入sed output

[oracle@ ~]$ cat test9

1apple

1banana

1mango

[oracle@ ~]$ cat test10

2orange

2strawberry

 [oracle@ ~]$ sed '2,$r test10' test9 –test10文件内容插入test9自第2行直至最后一行

1apple

1banana

2orange

2strawberry

1mango

2orange

2strawberry

[oracle@ ~]$ sed '$!r test10' test9—test10插入test9除最后1行的其余行之后

1apple

2orange

2strawberry

1banana

2orange

2strawberry

1mango

看完上述内容,你们对sed的基础用法是怎么样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Unix下sed的用法(二)
  2. Unix下sed的用法(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sed

上一篇:PostgreSQL Locks中LOCK结构体是什么

下一篇:docker镜像和容器的关系是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》