Search text files using regular expressions

«« Previous
Next »»

Regular Expressions


Although you will not be expected to know too much in the way of complex regular expressions for the  Linux Professional Institute Level 1 exam, you will need to know how to use regular expressions with the commands grep and sed. Being able to search text files in this way certainly will be of use you in your Linux career.

In the simplest form a regular expression is just a text string that you are searching for, such as: ‘jack‘. We can extend this to search for ‘[Jj]ack‘; where we now look for Jack or jack.

Grep


The GNU command grep can be used to search files for regular expressions. Grep stand for global regular expression print.

grep 'jack' file1
grep '[Jj]ack' file1

Using this regular expression we are only allowing for the first letter to upper or lowercase. If we are less certain on the case of the rest of the word we can also use grep -i to ignore case.

grep -i 'jack' file1

As we have seen before the square brackets represent a range of characters that we are looking for. Using these brackets we could search for numbers using ‘[0-9]‘ or for 4 consecutive numbers by adding a multiplier ‘[0-9]{4}‘ . To make use of the multiplier in the brace brackets we would need to use either egrep or grep -E.

Sed


Sed is the stream editor and like grep can be used to search files. Additionally, we can use the command to edit files, hence the name stream editor. To delete all commented lines from a file we can look at lines that begin with a #. To denote the start of a line we can use the ^ anchor. The –i option with sed is used for the in-place edit. The following command will edit file1 removing all commented lines.

sed -i  '/^#/d' file1

Take a look at the video where we step you through the process.


«« Previous
Next »»

0 comments:

Post a Comment