问题
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