Regular expression – parts matching with EGREP regular expression

I want to know, if I use egrep ((GNU grep)2.5.1), I can select a part of the matching text, for example:

grep'^([a-zA-Z.-]+)[0-9]+' ./file.txt

So I only get the matching part between the brackets< /p>

house.com

Instead of the whole line like I usually get:

house. com112

Suppose there is a line house.com112 in my file.txt.

(Actually this regular expression is just an example, I just want to know if I can only print the entire line Part of it.)

I know that in some languages, such as PHP, Perl or even AWK, I can do it, but I don’t know if I can use egrep.

Thank you in advance!

After grep finds a matching line, use sed to modify the result:

< p>

grep'^[a-zA-Z.-]+[0-9]+' ./file.txt | sed's/[0-9]\+$//' 

Or if you just want to stick to grep, you can use grep and the -o switch instead of sed:

grep'^[a-zA- Z.-]+[0-9]+' ./file.txt | grep -o'[a-zA-Z.-]+'

I want I know, if I use egrep((GNU grep)2.5.1), I can select a part of the matching text, for example:

grep'^([a-zA -Z.-]+)[0-9]+' ./file.txt

So I only get the matching part between the brackets

 house.com

Instead of the whole line like I usually get:

house.com112

Assuming my file. There is a line house.com112 in the txt.

(Actually this regular expression is just an example, I just want to know if I can only print part of the whole line.)

I know In some languages, such as PHP, Perl or even AWK, I can do it, but I don’t know if I can use egrep.

Thank you in advance!

After grep finds the matching line, use sed to modify the result:

grep'^ [a-zA-Z.-]+[0-9]+' ./file.txt | sed's/[0-9]\+$//'

Or if you just want Stick to grep, you can use grep and -o switch instead of sed:

grep'^[a-zA-Z.-]+[0-9]+' ./file.txt | grep -o'[a-zA-Z.-]+'

Leave a Comment

Your email address will not be published.