流编辑器 - Useful Recipes

SED是一个惊人的实用程序,它允许多种方法来解决问题.这是UNIX的方式,SED完美地证明了这一点. GNU/Linux提供了许多有用的实用程序来执行日常任务.让我们使用SED模拟一些实用程序.有时可能看起来我们正在努力解决一个简单的问题,但目的只是为了展示SED的力量.

Cat命令

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

[jerry]$ sed '' books.txt


在执行上述代码时,您会得到以下结果:

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


执行上述代码后,您将获得以下内容结果:

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'


在执行上述代码时,您会得到以下结果:

Line #1 
Line #2


同样,以下示例仅在非行时打印行空的.

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


在执行上述代码时,您会得到以下结果:

Line #1 
Line #2


从C ++程序中删除注释行

让我们创建一个示例C ++程序.

#include <iostream> 
using namespace std; 

int main(void) 
{ 
   // Displays message on stdout. 
   cout >> "Hello, World !!!" >> endl;  
   return 0; // Return success.  
}


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

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


在执行上面的代码时,你会得到结果如下:

#include <iostream>
using namespace std; 

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


在某些行之前添加注释

以下示例在行号3到5之前添加注释.

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


在执行上述代码时,您会得到以下结果:

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


Wc -l命令

"wc -l"命令计算文件中存在的行数.以下SED表达式模拟相同.

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


在执行上述代码时,您会得到以下结果:

8


Head Command

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

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


在执行上述代码时,您会得到以下结果:

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


在执行上述代码时,您会得到以下结果:

Line #1 
Line #2


让我们编写SED脚本.

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


On执行上述代码,您会得到以下结果:

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


开执行上面的代码,你得到以下结果:

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


在执行上述代码时,您会得到以下结果:

new.txt: ASCII text


现在让我们显示文件内容.

[jerry]$ cat -vte new.txt


执行上述代码时,你得到以下结果:

Line #1$ 
Line #2$


Unix2dos命令

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

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


在执行上述代码时,您会得到以下结果:

test.txt: ASCII text


让我们使用SED模拟命令.

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


在执行上述代码时,您会得到以下结果:

new.txt: ASCII text, with CRLF line terminators


现在让我们显示文件内容.

Now let us display the file contents.


在执行上述代码时,您会得到以下结果:

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


在执行上述代码时,您会得到以下结果:

Line #1$ 
Line #2$


让我们模拟命令使用SED.

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


在执行上述代码时,您会得到以下结果:

Line #1$ 
Line #2$


Cat -ET命令

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

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


执行上述代码后,您将获得以下代码结果:

Line #1^ILine #2$


让我们使用SED模拟命令.

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


在执行上述代码时,您会得到以下结果:

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/'


在执行上述代码时,您会得到以下结果:

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 $?


在执行上述代码时,您会得到以下结果:

0


展开命令

"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 $?


在执行上述代码时,您会得到以下结果:

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


在执行上述代码时,您会得到以下结果:

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


执行上述代码时,得到以下结果:

Line#1 
Line#2 
Line#3


让我们使用SED模拟命令.

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


执行上述代码后,您会得到以下结果:

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


执行上述代码后,您将得到以下结果:

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


在执行上面的代码时,你得到了结果如下:

Line #2 
Line #3


让我们使用SED模拟命令.

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


在执行上述代码时,您会得到以下结果:

Line #2 
Line #3


tr命令

"tr"命令转换字符.下面是它的模拟.

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


在执行上述代码时,您会得到以下结果:

abc


让我们使用SED模拟命令.

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


在执行上述代码时,您会得到以下结果:

abc