Sed实用功能

SED是一个了不起的工具,它允许多种方式来解决问题。 GNU/ Linux提供了许多有用的实用程序来执行日常的日常任务。让我们模拟使用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

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 

下面的示例使用打印命令来显示文件内容。

[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 命令

Unix的"wc -l" 命令的计数存在于文件中的行数。下面的sed表达式模拟相同。

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

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

8 

head 命令

默认情况下,Unix的head命令打印前3行的文件。让我们模拟Sed相同的行为。

[jerry]$ sed '3 q' books.txt 
执行上面的代码,会得到如下结果:
A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
The Alchemist, Paulo Coelho

tail -1 命令

Unix的“tail -1”打印文件的最后一行。下面的语法显示了模拟。

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

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

A Game of Thrones, George R. R. Martin

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模拟命令。

# Press "ctrl+v" followed "ctrl+m" to generate 
[jerry]$ sed 's/^M$//' test.txt > new.txt
"^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”命令行显示了由美元符号($)字符结束。下面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 命令

“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 命令

UNIXcat -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


联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3