本文介绍了使用grep解析文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为netlist.txt的文本文件,其内容如下:

I have a textfile called netlist.txt with the following contents:

M1      nmos1

M2      nmos2

P1      pmos1

        M3      nmos3

                M4      nmos4
                        P2      pmos2

我只想使用正则表达式检索以制表符/空格开头并匹配所有缩进的"M"值的行.

I want to retrieve only the line which starts with a tab/space and matching all the "M" values that are indented, using regex.

为此,我在bash中输入了以下表达式:

In order to accomplish this I entered the following expression in bash:

egrep [:space:]*[M][0-9]+ netlist.txt

但是它不能识别空格.它检索所有行,而不管是否有空格.请给我一些建议.谢谢,佩德罗

But it doesn't recognize the space. It retrieves all the lines regardless of having a space or not.Please give me some advice on this.Thanks,Pedro

推荐答案

您可以使用:

grep '^[[:blank:]]M[0-9]' file

输出:

        M3      nmos3

[[[:blank:]] 匹配行首的单个空格或单个制表符.另一方面, [[:: space:]] 与空格或制表符或换行符匹配.

[[:blank:]] matches either a single space or a single tab at line start. [[:space:]] on the other hand matches space or tab or newline.

这篇关于使用grep解析文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:24