Skip to content

Commit fe2f7b8

Browse files
committed
Update 教程.md
1 parent 3339e01 commit fe2f7b8

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

教程.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ RET=$?
393393

394394

395395
<h4>7. sed和正则表达式</h4>
396-
正则表达式是一种特殊的字符串,用来描述一串具有某种共同特征的字符串。比如,`*.txt`就用来描述所有文件扩展名为`txt`的文件。在进行批处理的时候,正则表达式有着异常强大的应用。
396+
正则表达式是一种特殊的字符串,用来描述一串具有某种共同特征的字符串。在进行批处理的时候,正则表达式有着异常强大的应用。
397397

398398
sed则是一个流编辑器(stream editor),它读入一个输出,并通过加工处理,输出经处理后的 文件/字符串 输出。下面我们通过一系列例子,来掌握sed的基本应用。
399399

@@ -451,6 +451,96 @@ sed -e "/^$/d" test-2.txt
451451

452452
再来看下面的语句:
453453

454+
```
455+
sed -e "1,3d" test-2.txt
456+
```
457+
458+
这个语句的意思,就是删除`test-2.txt`中的第一到第三行。观察输出可以看到,前三行被删除了。类似地,`sed -e "2,4!d" test-2.txt`则是指,除了2-4行,其他都删掉。`sed -e "d" test-2.txt` 则是删除文档里面的全部内容。请注意,为了避免频繁改动文档,以上几个命令都是用的`-e`,改动是体现在命令行的输出的。如果要直接对文档进行改动,请用`-i.tmp`,或者分开写为`-i '.tmp'`。在Mac OS X的Bash Shell里面,似乎提供一个备份文件的扩展名是必须的,而在Linux平台则似乎是可选的。
459+
460+
更为常见的`sed`的应用,是用它来进行替换。看看下面的例子:
461+
```
462+
sed -e 's/But/but/' test-2.txt
463+
```
464+
465+
命令行的输出:
466+
467+
```
468+
This is file with several lines
469+
some of which are blank lines
470+
for example, the line that follows is blank
471+
472+
but this line has several characters.
473+
474+
And this marks the end of the file.
475+
```
476+
477+
可以观察到,首字母大写的`But`被替换成`but`。替换的基本格式是`'s/a/b/'``a``b`分别代表替换前和替换后。我们再来多看几个例子,首先将`test-2.txt`文档的内容改为:
478+
479+
```
480+
1.This is file with several lines
481+
2.some of which are blank lines
482+
for example, the line that follows is blank
483+
484+
7.But this line has several characters.
485+
486+
And this marks the end of the file.
487+
```
488+
489+
第三行和最后一行前面有一个空格。接着我们在`tutorial.sh`里面加入以下命令:
490+
```
491+
sed -i '.tmp' 's/^[ 1-3]//' test-2.txt
492+
```
493+
494+
你会看到以下输出:
495+
496+
```
497+
.This is file with several lines
498+
.some of which are blank lines
499+
for example, the line that follows is blank
500+
501+
7.But this line has several characters.
502+
503+
And this marks the end of the file.
504+
```
505+
506+
在这个例子里,正则表达式`^[ 1-3]``^`表示字符串的开始,而中括号表示匹配任意一个在中括号里面的字符。我们的中括号里面有空格以及数字1-3,而之后`*`表示零到任意多个任意字符。于是,`sed`根据正则表达式的要求去逐行扫描,找出“以空格或者数字1-3的行”。
507+
508+
找到之后干什么呢?这就要看第二个`/`后面的内容了,而我们发现第二个`/`和第三个`/`之间并没有内容。这是说,找到了符合要求的这个字符串,就将其替换为空字符。于是你可以看到以上的输出了。第一行第二行的数字被移除,第三行和最后一行的空格被移除,而第五行的数字7则不受影响。
509+
510+
保留新的`test-2.txt`,我们继续执行以下命令:
511+
512+
```
513+
sed -i '.tmp' 's/[!.]$/;/' test-2.txt
514+
```
515+
516+
这个命令则是找出以`!` `.`结尾的行,并一律改为以分号结尾。
517+
518+
我们继续在原有的文档操作,输入以下命令:
519+
520+
```
521+
sed -i '.tmp' 's/^[. 1-9]*//;s/[;.!]$/ ENDING/' test-2.txt
522+
523+
```
524+
525+
这是两个替换命令一起来。首先,我们找出以`.` 或者数字1-9,或者空格开头的行,然后将其删掉。请注意,这里有一个`*`,它代表了任意多个(包括0)前一个字符的重复。比如如果一个行是以三个空格开头的,则加上`*`可以加之一并铲除,如果不加的话就只会匹配并删除第一个空格。两个合并命令之间以空格隔开。第二个命令是找出以感叹号句号或者感叹号结尾的行,代之以` ENDING`
526+
527+
输出结果应该是:
528+
529+
```
530+
This is file with several lines
531+
some of which are blank lines
532+
for example, the line that follows is blank
533+
534+
But this line has several characters ENDING
535+
536+
And this marks the end of the file ENDING
537+
```
538+
539+
540+
541+
542+
543+
454544

455545

456546

0 commit comments

Comments
 (0)