Linux总结(十四):linux文本处理工具——基本sed

2024-04-28 05:18

本文主要是介绍Linux总结(十四):linux文本处理工具——基本sed,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、sed脚本定义

        Vim 采用的是交互式文本编辑模式,可以用键盘命令来交互性地插入、删除或替换数据中的文本。 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据。

1、sed的特点

          (1)sed 会根据脚本命令来处理文本文件中的数据,这些命令要么从命令行中输入,要么存储在一个文本文件中 。

          (2)每次仅读取一行内容;

          (3)根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;

          (4)将执行结果输出。

2、sed基本格式

[root@localhost ~]# sed [选项] [脚本命令] 文件名

注意:本章从“二”到“十”的,带单引号的都是脚本命令。

sed 命令常用选项及含义
选项含义
-e 脚本命令该选项会将其后跟的脚本命令添加到已有的命令中。
-f 脚本命令文件该选项会将其后文件中的脚本命令添加到已有的命令中。
-nsed 会自动内容,这个屏蔽输出,需使用 print 命令来完成输出。
-i此选项会直接修改源文件。

二、sed s 替换脚本命令

1、s替换基本格式

[address]s/pattern/replacement/flags

              注释: (1)address 表示指定要操作的行。

                          (2)pattern 指的是需要替换的内容。

                          (3)replacement 指的是要替换的新内容。 

                          (4)flags如下:

s替换的flags 标记功能
n1~512 之间的数字,如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个“2”标记;
g对所有匹配到的内容进行替换,当无 g则只在第一次匹配成功时做替换操作。
p打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。
w file将缓冲区中的内容写到指定的 file 文件中;
&用正则表达式匹配的内容进行替换;
\n匹配第 n 个子串,该子串之前在 pattern 中用 \(\) 指定。
\转义(转义替换部分包含:&、\ 等)。

2、 具体应用

"1、sed 编辑器只替换每行中第 2 次出现的匹配模式"
[root@localhost ~]sed ‘s/test/trial/2’ data4.txt
#This is a test of the trial script.
#This is the second test of the trial script.
"2、用新文件替换所有匹配的字符串,可以使用 g 标记"
[root@localhost ~]sed ‘s/test/trial/g’ data4.txt
#This is a trial of the trial script.
#This is the second trial of the trial script.
"3、-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行"
[root@localhost ~]cat data5.txt
#This is a test line.
#This is a different line.
[root@localhost ~]sed -n ‘s/test/trial/p’ data5.txt
#This is a trial line.
"4、w 标记会将匹配后的结果保存到指定文件中"
[root@localhost ~]sed ‘s/test/trial/w test.txt’ data5.txt
#This is a trial line.
#This is a different line.
[root@localhost ~]cat test.txt
#This is a trial line.
"5、使用 s 脚本命令时,替换类似文件路径麻烦,需要转义"
[root@localhost ~]sed ‘s/\/bin\/bash/\/bin\/csh/’ /etc/passwd

 三、sed d 删除脚本命令

        如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。如果忘记指定具体行的话,文件中的所有内容都会被删除。

1、基本格式

[address]d 

2、基本应用

"1、不指定行,将删除所有行"
[root@localhost ~]cat data1.txt
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy dog
[root@localhost ~]sed ‘d’ data1.txt
#什么也不输出,证明成了空文件
"2、删除 data6.txt 文件内容中的第 3 行"
[root@localhost ~]cat data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is line number 4.
[root@localhost ~]sed ‘3d’ data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 4.
"3、删除 data6.txt 文件内容中的第 2、3行"
[root@localhost ~]sed ‘2,3d’ data6.txt
#This is line number 1.
#This is line number 4.
"4、通过特殊的文件结尾字符,比如删除 data6.txt 文件内容中第 3 行开始的所有的内容"
[root@localhost ~]sed ‘3,$d’ data6.txt
#This is line number 1.
#This is line number 2.

               注意:默认情况下 sed 并不会修改原始文件,这里被删除的行只是从 sed 的输出中消失了,原始文件没做任何改变。

四、sed的“a”(附加)和“i”(插入)指令

a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行

1、基本格式

[address]a(或 i)\新文本内容 

2、基本用法

"1、将一个新行插入到数据流第三行前"
[root@localhost ~]sed ‘3i\
> This is an inserted line.’ data6.txt
#This is line number 1.
#This is line number 2.
#This is an inserted line.
#This is line number 3.
#This is line number 4.
"2、将一个新行附加到数据流中第三行后"
[root@localhost ~]sed ‘3a\
> This is an appended line.’ data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is an appended line.
#This is line number 4.
"3、将一个多行数据添加到数据流中,只需对要插入或附加的文本中的每一行末尾(除最后一行)添加反斜线即可"
[root@localhost ~]sed ‘1i\
> This is one line of new text.\
> This is another line of new text.’ data6.txt
#This is one line of new text.
#This is another line of new text.
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is line number 4.

 五、sed c 替换脚本命令

前边s是替换某个词,这里c 是替换一整行

1、基本格式

[address]c\用于替换的新文本 

2、基本应用

"1、sed 编辑器会修改第三行中的文本"
[root@localhost ~]sed ‘3c\
> This is a changed line of text.’ data6.txt
#This is line number 1.
#This is line number 2.
#This is a changed line of text.
#This is line number 4.
"2、相同功能"
[root@localhost ~]sed ‘/number 3/c\
> This is a changed line of text.’ data6.txt
#This is line number 1.
#This is line number 2.
#This is a changed line of text.
#This is line number 4.

 六、sed y 转换脚本命令

1、基本格式

[address]y/inchars/outchars/

       转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。 

2、基本应用

[root@localhost ~]sed 'y/123/789/' data8.txt
#This is line number 7.
#This is line number 8.
#This is line number 9.
#This is line number 4.
#This is line number 7 again.
#This is yet another line.
#This is the last line in the file.

       inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相同位置的那个字符。转换命令是一个全局命令,也就是说,它会文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置。

[root@localhost ~]echo "This 1 is a test of 1 try." | sed 'y/123/456/'
#This 4 is a test of 4 try.

七、sed p 打印脚本命令

p 命令表示搜索符号条件的行,并输出该行的内容

1、基本格式

[address]p

2、基本应用

"1、用 -n 选项和 p 命令配合使用,我们可以禁止输出其他行,只打印包含匹配文本模式的行"
[root@localhost ~]cat data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is line number 4.
[root@localhost ~]sed -n ‘/number 3/p’ data6.txt
#This is line number 3.
"2、在修改行之前显示该行"
[root@localhost ~]sed -n ‘/3/{
> p
> s/line/test/p
> }’ data6.txt
#This is line number 3.
#This is test number 3.

八、sed w 脚本命令

w 命令用来将文本中指定行的内容写入文件中

1、基本格式

[address]w filename 

            filename 表示文件名,可以使用相对路径或绝对路径,但不管是哪种,运行 sed 命令的用户都必须有文件的写权限。 

2、基本使用

"1、将数据流中的前两行打印到一个文本文件中"
[root@localhost ~]sed '1,2w test.txt' data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is line number 4.
[root@localhost ~]cat test.txt
#This is line number 1.
#This is line number 2.
"2、如果不想让行直接输出,可以用 -n 选项"
[root@localhost ~]cat data11.txt
#Blum, R       Browncoat
#McGuiness, A  Alliance
#Bresnahan, C  Browncoat
#Harken, C     Alliance
[root@localhost ~]sed -n '/Browncoat/w Browncoats.txt' data11.txt
[root@localhost ~]cat Browncoats.txt
#Blum, R       Browncoat
#Bresnahan, C  Browncoat

九、sed r 脚本命令

r 命令用于将一个独立文件的数据插入到当前数据流的指定位置。r是插入到行后,i是行前

 1、基本格式

[address]r filename

2、基本用法

'1、sed 命令会将 filename 文件中的内容插入到 address 指定行的后面'
[root@localhost ~]cat data12.txt
#This is an added line.
#This is the second added line.
[root@localhost ~]sed '3r data12.txt' data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is an added line.
#This is the second added line.
#This is line number 4.
"2、如果你想将指定文件中的数据插入到数据流的末尾,可以使用 $ 地址符"
[root@localhost ~]sed '$r data12.txt' data6.txt
#This is line number 1.
#This is line number 2.
#This is line number 3.
#This is line number 4.
#This is an added line.
#This is the second added line.

 十、sed q 退出脚本命令

q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理。

1、基本使用

"1、使用 q 命令之后,sed 命令会在匹配到 number 1 时,
将其替换成 number 0,然后直接退出"
[root@localhost ~]sed '2q' test.txt
#This is line number 1.
#This is line number 2.
[root@localhost ~]sed '/number 1/{ s/number 1/number 0/;q; }' test.txt
#This is line number 0.

 十一、sed 脚本命令的寻址方式

      我们一直忽略了对 address 部分的介绍。对各个脚本命令来说,address 用来表明该脚本命令作用到文本中的具体行。

注意:

      默认情况下,sed 命令会作用于文本数据的所有行(之前的都是默认情况下,作用在所有行)。如果只想将命令作用于特定行或某些行,则必须写明 address 部分,表示的方法有以下 2 种:

            (1)以数字形式指定行区间;

            (2)用文本模式指定具体行区间。

     确定address的两种格式:

             (1)[address]脚本命令
             (2)address {
                             多个脚本命令
                       }

 1、以数字形式指定行区间

        当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed 会将文本流中的第一行编号为 1,然后继续按顺序为接下来的行分配行号。在脚本命令中,指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里举一个 sed 命令作用到指定行号的例子:

"1、指定的地址可以是单个行号"
[root@localhost ~]sed '2s/dog/cat/' data1.txt
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy cat
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy dog
"2、使用了行地址区间"
[root@localhost ~]sed '2,3s/dog/cat/' data1.txt
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy cat
#The quick brown fox jumps over the lazy cat
#The quick brown fox jumps over the lazy dog
"3、如果想将命令作用到文本中从某行开始的所有行,可以用特殊地址——美元符($)"
[root@localhost ~]sed '2,$s/dog/cat/' data1.txt
#The quick brown fox jumps over the lazy dog
#The quick brown fox jumps over the lazy cat
#The quick brown fox jumps over the lazy cat
#The quick brown fox jumps over the lazy cat

 2、用文本模式(正则表达式)指定行区间

1、基本格式

/pattern/command

          注意,必须用正斜线将要指定的 pattern 封起来,sed 会将该命令作用到包含指定文本模式的行上。

2、基本应用

[root@localhost ~]# grep demo /etc/passwd
demo:x:502:502::/home/Samantha:/bin/bash
[root@localhost ~]# sed '/demo/s/bash/csh/' /etc/passwd
"/demo/正则表达式,s是替换文本命令"
root:x:0:0:root:/root:/bin/bash
...
demo:x:502:502::/home/demo:/bin/csh
...

 3、文本模式用正则表达式

        正则表达式允许创建高级文本模式匹配表达式来匹配各种数据。这些表达式结合了一系列通配符、特殊字符以及固定文本字符来生成能够匹配几乎任何形式文本的简练模式。

 

这篇关于Linux总结(十四):linux文本处理工具——基本sed的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942407

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

揭秘未来艺术:AI绘画工具全面介绍

📑前言 随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面。在艺术创作领域,AI技术同样展现出了其独特的魅力。今天,我们就来一起探索这个神秘而引人入胜的领域,深入了解AI绘画工具的奥秘及其为艺术创作带来的革命性变革。 一、AI绘画工具的崛起 1.1 颠覆传统绘画模式 在过去,绘画是艺术家们通过手中的画笔,蘸取颜料,在画布上自由挥洒的创造性过程。然而,随着AI绘画工

墨刀原型工具-小白入门篇

墨刀原型工具-小白入门篇 简介 随着互联网的发展和用户体验的重要性越来越受到重视,原型设计逐渐成为了产品设计中的重要环节。墨刀作为一款原型设计工具,以其简洁、易用的特点,受到了很多设计师的喜爱。本文将介绍墨刀原型工具的基本使用方法,以帮助小白快速上手。 第一章:认识墨刀原型工具 1.1 什么是墨刀原型工具 墨刀是一款基于Web的原型设计工具,可以帮助设计师快速创建交互原型,并且可以与团队

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

十五.各设计模式总结与对比

1.各设计模式总结与对比 1.1.课程目标 1、 简要分析GoF 23种设计模式和设计原则,做整体认知。 2、 剖析Spirng的编程思想,启发思维,为之后深入学习Spring做铺垫。 3、 了解各设计模式之间的关联,解决设计模式混淆的问题。 1.2.内容定位 1、 掌握设计模式的"道" ,而不只是"术" 2、 道可道非常道,滴水石穿非一日之功,做好长期修炼的准备。 3、 不要为了

十四、观察者模式与访问者模式详解

21.观察者模式 21.1.课程目标 1、 掌握观察者模式和访问者模式的应用场景。 2、 掌握观察者模式在具体业务场景中的应用。 3、 了解访问者模式的双分派。 4、 观察者模式和访问者模式的优、缺点。 21.2.内容定位 1、 有 Swing开发经验的人群更容易理解观察者模式。 2、 访问者模式被称为最复杂的设计模式。 21.3.观察者模式 观 察 者 模 式 ( Obser