Parsing text using grep

别来无恙 提交于 2021-01-29 04:55:27

问题


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

M1      nmos1

M2      nmos2

P1      pmos1

        M3      nmos3

                M4      nmos4
                        P2      pmos2

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

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


回答1:


You can use:

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

Output:

        M3      nmos3

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



来源:https://stackoverflow.com/questions/35058144/parsing-text-using-grep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!