Regular expression with egrep and ls

October 7th, 2006 mysurface Posted in egrep, grep, ls, Regular Expression, Symbol, Text Manipulation | Hits: 79698 | 3 Comments »

egrep is actually same as grep -E, it interpret PATTERN as an extended regular expression. Therefore ESCAPE ( \ ) is not needed for certain symbol.

ls is a common tool for listing file that matches the keyword.

Regular expression (RE) is a very important and useful to manipulating strings and text. In this example, I would like to introduce few RE symbols.
Asterisk ( * ) – This matches any number (including ZERO) of repeats of the character string.

ls "file1234*"

This will return files with filename: file123, file1234, file 1223444.

Dot ( . ) – This will matches any one character, but except newline.

ls "file1234."

This will return files with filename: file1234q, file12345, but not file1234

Caret ( ^ ) – This indicate begin of line, but sometimes it indicate negative too.
Dollar Sign ( $ ) – This indicate end of line, but sometimes it indicate value of variable too.

If you want to grep “void” at the begin of lines, you do this:

 egrep "^void" myhello.c 

If you want to grep “)” at the end of lines, you do this:

 egrep "\)" myhello.c 

You need to put escape because “)” is a special symbol.

To filter out the blank lines, you do this:

 grep -v "^$" mysetting.conf 

3 Responses to “Regular expression with egrep and ls”

  1. [...] Previously we have an example on regular expression, but It doesn’t shows the power of square brackets ( [ ] ) [...]

  2. rattlesnake Says:

    little helpful… but what about *,+ and ?

    where are they ?

  3. I have to thank you for the efforts you have put in penning this website. I’m hoping to check out the same high-grade content by you later on as well. In truth, your creative writing abilities has motivated me to get my very own blog now ;)

Leave a Reply