Live Chat!

Regular expression with egrep and ls

* * *     2 votos

October 7th, 2006 mysurface Posted in Regular Expression, Symbol, Text Manipulation, egrep, grep, ls | Hits: 15271 |

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 

One Response 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 ( [ ] ) [...]

Leave a Reply