Sed - 实用工具

Sed - 实用工具 首页 / Sed入门教程 / Sed - 实用工具

SED是一个了不起的实用程序,它允许使用多种方法来解决问题。这是UNIX方式,SED完美地证明了这一点。 GNU/Linux提供了许多有用的实用程序来执行日常任务,让无涯教程使用SED模拟一些实用程序。

Cat 命令

在以下示例中,每行都作为默认工作流程的一部分进行打印。

[jerry]$sed '' books.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

A Storm of Swords, George R. R. Martin 
The Two Towers, J. R. R. Tolkien 
The Alchemist, Paulo Coelho 
The Fellowship of the Ring, J. R. R. Tolkien 
The Pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin 

以下示例使用print命令显示文件内容。

[jerry]$sed -n 'p' books.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

A Storm of Swords, George R. R. Martin 
The Two Towers, J. R. R. Tolkien 
The Alchemist, Paulo Coelho 
The Fellowship of the Ring, J. R. R. Tolkien 
The Pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin

删除空行

在以下示例中," ^ $"表示空行,并且当模式匹配成功时,空行将被删除。

[jerry]$echo -e "Line #1\n\n\nLine #2" | sed '/^$/d'

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1 
Line #2 

同样,下面的示例仅在非空行时打印该行。

[jerry]$echo -e "Line #1\n\n\nLine #2" | sed -n '/^$/!p'

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1 
Line #2

删除C++注释

创建一个示例C++程序。

#include <iostream> 
using namespace std; 

int main(void) 
{ 
   //在标准输出上显示消息。
   cout >> "Hello, World !!!" >> endl;  
   return 0; //Return success. 
}

现在,使用以下正则表达式删除注释。

[jerry]$sed 's|//.*||g' hello.cpp

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

#include <iostream>
using namespace std; 

int main(void) 
{ 
   cout >> "Hello, World !!!" >> endl; 
   return 0;  
} 

添加注释

下面的示例在第3到5行之前添加注释。

[jerry]$sed '3,5 s/^/#/' hello.sh 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

#!/bin/bash 
#pwd 
#hostname 
#uname -a 
who 
who -r 
lsb_release -a

WC -l 命令

" wc -l"命令计算文件中存在的行数,以下SED表达式对此进行了模拟。

[jerry]$sed -n '$=' hello.sh 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

8 

Head 命令

默认情况下,head命令打印文件的前10行,用SED模拟相同的行为。

[jerry]$sed '10 q' books.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

A Storm of Swords 
George R. R. Martin 
The Two Towers 
J. R. R. Tolkien 
The Alchemist 
Paulo Coelho 
The Fellowship of the Ring 
J. R. R. Tolkien 
The Pilgrimage
Paulo Coelho

Tail -1 命令

" tail -1"打印文件的最后一行。以下语法显示了其模拟。

[jerry]$echo -e "Line #1\nLine #2" > test.txt 
[jerry]$cat test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1 
Line #2 

让无涯教程编写SED脚本。

[jerry]$sed -n '$p' test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #2 

Dos2unix 命令

在DOS环境中,换行符由CR/LF字符的组合表示。下面的" dos2unix"命令模拟将DOS换行符转换为UNIX换行符。在GNU/Linux中,此字符通常被视为" ^ M"(控制M)字符。

[jerry]$echo -e "Line #1\r\nLine #2\r" > test.txt 
[jerry]$file test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

test.txt: ASCII text, with CRLF line terminators 

使用SED模拟命令。

[jerry]$sed 's/^M$//' test.txt > new.txt   # Press "ctrl+v" followed "ctrl+m" to generate 
"^M" character. 
[jerry]$file new.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

new.txt: ASCII text 

现在显示文件内容。

[jerry]$cat -vte new.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1$
Line #2$

Unix2dos 命令

与" dos2unix"相似,有一个" unix2dos"命令可将UNIX换行符转换为DOS换行符。以下示例显示了相同的模拟。

[jerry]$echo -e "Line #1\nLine #2" > test.txt 
[jerry]$file test.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

test.txt: ASCII text

让无涯教程使用SED模拟命令。

[jerry]$sed 's/$/\r/' test.txt  > new.txt 
[jerry]$file new.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

new.txt: ASCII text, with CRLF line terminators

现在显示文件内容。

Now let us display the file contents.

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1^M$
Line #2^M$

Cat -E 命令

" cat -E"命令以Dollar($)字符显示行尾。以下SED示例是相同的模拟。

[jerry]$echo -e "Line #1\nLine #2" > test.txt 
[jerry]$cat -E test.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1$
Line #2$

让无涯教程使用SED模拟命令。

[jerry]$sed 's|$|&$|' test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1$
Line #2$

Cat -ET 命令

" cat -ET"命令在每行末尾显示Dollar($)符号,并将TAB字符显示为" ^ I"。以下示例显示了使用SED对" cat -ET"命令的模拟。

[jerry]$echo -e "Line #1\tLine #2" > test.txt 
[jerry]$cat -ET test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1^ILine #2$

使用SED模拟命令。

[jerry]$sed -n 'l' test.txt | sed 'y/\\t/^I/'

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1^ILine #2$

nl 命令

" nl "命令仅对文件行编号。下面的SED脚本模拟了此行为。

[jerry]$echo -e "Line #1\nLine #2" > test.txt 
[jerry]$sed=test.txt | sed 'N;s/\n/\t/'

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

1 Line #1 
2 Line #2

第一个SED表达式打印行号,然后打印它们的内容,第二个SED表达式合并这两行并将换行符转换为TAB字符。

cp 命令

cp"命令创建文件的另一个副本。下面的SED脚本模拟了此行为。

[jerry]$sed -n 'w dup.txt' data.txt 
[jerry]$diff data.txt dup.txt 
[jerry]$echo $? 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

0

Expand 命令

"expand"命令将TAB字符转换为空格。以下代码显示了其仿真。

[jerry]$echo -e "One\tTwo\tThree" > test.txt 
[jerry]$expand test.txt > expand.txt 
[jerry]$sed 's/\t//g' test.txt > new.txt 
[jerry]$diff new.txt expand.txt  
[jerry]$echo $? 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

0 

Tee 命令

"tee"命令将数据转储到标准输出流以及文件中。下面给出的是" tee"命令的模拟。

[jerry]$echo -e "Line #1\nLine #2" | tee test.txt  
Line #1 
Line #2 

使用SED模拟命令。

[jerry]$sed -n 'p; w new.txt' test.txt  

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1 
Line #2

Cat -s 命令

UNIX" cat -s"命令禁止重复的空输出行。以下代码显示了对" cat -s"命令的模拟。

[jerry]$echo -e "Line #1\n\n\n\nLine #2\n\n\nLine #3" > test.txt  
[jerry]$cat -s test.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1  
Line #2
Line #3

使用SED模拟命令。

[jerry]$sed '1s/^$//p;/./,/^$/!d' test.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1  
Line #2  
Line #3 

Grep 命令

默认情况下,模式匹配成功时," grep"命令将打印一行。以下代码显示了其仿真。

[jerry]$echo -e "Line #1\nLine #2\nLine #3" > test.txt  
[jerry]$grep "Line #1" test.txt 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #1

让无涯教程使用SED模拟命令。

[jerry]$sed -n '/Line #1/p' test.txt 

执行上述代码后,您将得到以下输出:

Line #1 

Grep -v 命令

默认情况下,模式匹配失败时," grep -v "命令将打印一行。以下代码显示了其仿真。

[jerry]$echo -e "Line #1\nLine #2\nLine #3" > test.txt  
[jerry]$grep -v "Line #1" test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #2 
Line #3 

让无涯教程使用SED模拟命令。

[jerry]$sed -n '/Line #1/!p' test.txt

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

Line #2 
Line #3

Tr 命令

"tr  "命令翻译字符。下面给出了其模拟。

[jerry]$echo "ABC" | tr "ABC" "abc" 

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

abc

使用SED模拟命令。

[jerry]$echo "ABC" | sed 'y/ABC/abc/'

执行上述代码后,您将得到以下输出:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-useful-recipes.html

来源:LearnFk无涯教程网

abc

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出计算机组成原理 -〔徐文浩〕

ZooKeeper实战与源码剖析 -〔么敬国〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

现代C++编程实战 -〔吴咏炜〕

图解 Google V8 -〔李兵〕

Spark性能调优实战 -〔吴磊〕

讲好故事 -〔涵柏〕

全链路压测实战30讲 -〔高楼〕

大厂广告产品心法 -〔郭谊〕

好记忆不如烂笔头。留下您的足迹吧 :)