linux 命令之sed

sed:用于过滤和转换文件的流编辑器

执行方式: sed [options] 'command' [filename]   

                   sed [options] '/pattern/ command'

                   sed [options] 'n,m command'  (n,m为行号)

command(命令):

s/re/string/   : 替换  使用字符串string替换正则表达式re的内容 (只替换第一个)

#echo hello gino hello wrold | sed 's/\<hello\>/Hello/'
Hello gino hello wrold

g :行内全部替换

# echo hello gino hello wrold | sed 's/\<hello\>/Hello/g'
Hello gino Hello wrold
匹配样式的行进行替换示例 :
# cat sample_one 
one 	1
two	1
three	1
one	1
two	1
two	1
three	1

# sed '/two/ s/1/2/' sample_one 
one 	1
two	2
three	1
one	1
two	2
two	2
three	1


d : 删除行

# sed '/two/ d' sample_one 
one 	1
three	1
one	1
three	1
# sed '/two/ !d' sample_one  #删除匹配以外的行 
two	1
two	1
two	1
# sed '1,5 d' sample_one    #删除第1到第5行
two	1
three	1


i\ :插入文本(行前)

# sed '5 i\hello gino' sample_one 
one 	1
two	1
three	1
one	1
hello gino
two	1
two	1
three	1

a\ :追加文本(行后)

# sed '5 a\hello gino' sample_one 
one 	1
two	1
three	1
one	1
two	1
hello gino
two	1
three	1


c\ : 替换文本(本行)

# sed '5 c\hello gino' sample_one 
one 	1
two	1
three	1
one	1
hello gino
two	1
three	1


p : 打印

# sed '5 p' sample_one one 	1
two	1
three	1
one	1
two	1
two	1
two	1   #打印出来的一行
three	1


w : 写入到文件结尾

# sed -e '/two/ s/1/2/' -e '/three/ s/1/3/' -e '1,3 w sample_two' sample_one   
one		1
two		2
three		3
one		1
two		2
two		2
three		3
# cat sample_two 
one		1
two		2
three		3


q : 提前退出

# sed '3q' sample_one #处理到第三行就退出sed命令
one		1
two		1
three		1





options(选项):

-f : 从文件中读取替换规则

# cat replace_list /two/ s/1/2/
/three/ s/1/3/
# sed -f replace_list sample_one 
one 	1
two	2
three	3
one	1
two	2
two	2
three	3

-n : 禁止显示

# sed -n '/two/ d' sample_one   #不显示结果



-i  :对文件进行操作(不加此选项 不改变文件的内容,只改变显示的结果而已)

# cat sample_one 
two	1
#sed -i 'p' sample_one  
# cat sample_one   #源文件被改变了
two	1
two	1


-e : 执行多个sed

# sed -e '/two/ s/1/2/' -e '/three/ s/1/3/' sample_one 
one		1
two		2
three		3
one		1
two		2
two		2
three		3

ubuntu还支持另外一种 执行多个sed 的语法(其他linux系统没尝试)

 

# sed '/two/ s/1/2/;/three/ s/1/3/' sample_one 
one		1
two		2
three		3
one		1
two		2
two		2
three		3



### 回答1: sed是一个强大的文本处理工具,常用于从标准输入或文件中读取文本,进行编辑和转换,并将输出写入标准输出或文件中。 以下是sed常用的命令选项: 1. -n:只显示经过sed编辑的行 2. -i:直接修改文件内容,不输出到终端 3. -e:允许多个命令一起执行 4. -f:从文件中读取sed命令进行处理 以下是sed常用的命令: 1. s:替换。语法为:s/要替换的内容/替换后的内容/g,其中g表示全局替换。 2. p:打印。将指定的行或所有行输出到终端。 3. d:删除。删除指定的行或所有行。 4. a:追加。在指定行后面添加新的内容。 5. i:插入。在指定行前面插入新的内容。 6. w:写入。将指定行写入到文件中。 举例说明: 1. 将文件中的所有"cat"替换为"dog":sed -i 's/cat/dog/g' file.txt 2. 只显示文件中包含“hello”的行:sed -n '/hello/p' file.txt 3. 删除文件中的第3行:sed -i '3d' file.txt 4. 在文件中第2行后面添加一行内容:“hello world”:sed -i '2a hello world' file.txt 5. 将文件中第1行替换为“hello”并保存到另一个文件中:sed '1s/.*/hello/' file.txt > newfile.txt ### 回答2: sed是一种常用的Linux命令,其全称为Stream Editor,用于对文本进行处理和转换。它可以在命令行中以各种方式对文件内容进行修改,包括替换、插入、删除、追加等操作。 sed的基本语法是`s/old/new/`,其中old表示待替换的字符串,new表示替换后的内容。sed命令会读取文件的内容,并对匹配到的old进行替换。如果需要对整个文件进行替换,则需要加上选项`-i`。 除了替换以外,sed还可以实现很多其他的文本操作。例如,通过`d`选项可以删除匹配到的行;通过`i`选项可以在指定行前插入文本;通过`a`选项可以在指定行后追加文本等。 另外,sed命令还可以使用正则表达式来进行模式匹配。通过在替换语句中使用正则表达式,可以对符合特定模式的字符串进行批量替换。 总之,sed是一种非常强大和灵活的文本处理工具,可以方便地对文件内容进行修改和转换,提高工作效率。熟练掌握sed命令可以帮助我们更好地处理和编辑文本文件。 ### 回答3: sed(Stream Editor)是Linux中常用的文本处理工具之一。它可以对文本进行流式编辑,具体操作包括查找、删除、替换、插入行或字符等。 使用sed命令的基本语法为: ``` sed [选项] '动作' 文件名 ``` 其中,选项可以包括: - -n:只显示或处理特定行; - -e:指定要执行的动作; - -f:从指定文件中读取动作。 动作部分可以包括: - 数字n:表示打印第n行; - p:打印匹配行; - d:删除匹配行; - s/old/new/:查找并替换文本; - a\text:在匹配行后插入文本; - i\text:在匹配行前插入文本。 以下是一些常用的sed命令示例: - 打印文件内容: ``` sed 'p' file.txt ``` - 删除匹配行: ``` sed '/pattern/d' file.txt ``` - 查找并替换文本: ``` sed 's/old/new/' file.txt ``` - 打印指定行数: ``` sed -n 'n' file.txt ``` - 在匹配行后插入文本: ``` sed '/pattern/a\text' file.txt ``` - 在匹配行前插入文本: ``` sed '/pattern/i\text' file.txt ``` 通过灵活运用sed命令,我们可以轻松地对文本文件进行各种编辑操作,提高工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值