And remember that grep can be used to search some strings in plain-text data sets like files. The name comes from g/re/p (globally search a regular expression and print) which it does exactly within the plain text. In some cases we will use the egrep, extended version that supports extended regular expression, some of which we will define here.

Let's go over the regular expressions:

. (dot) - a single character.

grep peach. fruits.txt

how does that differ from

grep peach fruits.txt

can you notice the difference?

? - the preceding character matches 0 or 1 times only.

Try the same search with question mark egrep peach? fruits.txt

Let's try egrep peach??? fruits.txt

This works, why? Think about it a little bit.

* - the preceding character matches 0 or more times.

Let's see the meaning of this in two examples:

grep p fruits.txt

and

grep p* fruits.txt

Why are the different? Read the definition again and it should be obvious.

+ - the preceding character matches 1 or more times.

{n} - the preceding character matches exactly n times.

{n,m} - the preceding character matches at least n times and not more than m times.

[agd] - the character is one of those included within the square brackets.

[^agd] - the character is not one of those included within the square brackets.

[c-f] - the dash within the square brackets operates as a range. In this case it means either the letters c, d, e or f.

Let's see some examples

Try egrep p{3} fruits.txt to find the patterns that have at least three p inside.

Or search for a b or c in the file: egrep [a-c] fruits.txt

Note that [c-f1-9] matches any one of the characters in the ranges c to f and 1 to 9 (takes the union), for instance, [a-z0-9] matches all the lowercase letters or any digit.

Combined sequences of bracketed characters match common word patterns. [Hh][Ee][Yy] matches hey, Hey, HEY, and so on. (Q: How does that differ from [HhEeYy] ?)

Let try it egrep [ao][pr] fruits.txt

^ - matches the beginning of the line (in some cases, negates the meaning of the set, see above for one case).

$ - matches the end of the line.

\x - matches the character x, where the character's special meaning is stripped by the backslash.

\ - matches a backslash (strip the special meaning of the second backslash).

Try egrep 'es$' fruits.txt to find patterns that end with "es"

Some more extended patterns (may be available for non-POSIX compliant systems)

This will search for patterns that have alphanumeric characters (here, all will have it). Let's search for digits:

grep [[:digit:]] fruits.txt

Sed

Sed is the 'stream editors'.

Let's use for replacements in the strings. Note that the replacements are done on the streams, not the files themselves.

Let's fix a typo.

sed 's/pppppp/p/g' fruits.txt

Let's replace limes with lemons:

sed 's/lime/lemon/g' fruits.txt

Let's just see the replacements (read your notes about this part please)

sed -n 's/lime/lemon/p' fruits.txt

Let's add an extra line after each line:

sed -n 's/$/\n/p' fruits.txt

This one was a little advanced, please make sure that this does what you expect.

sed 's/\(\b[a-z]\)/\(\1\)/g' fruits.txt

A little more advanced one, this has multiple properties:

\b is the word boundary

\( and \) does a grouping, where you can later use via \1

[a-z] is the letters from a-z

All combined, this says get the first small caps letter at the beginning of each word \(\b[a-z]\) into a group (will be used \1 in the replacement part) and wrap it with parenthesis (Q: Why doesn't the \( \) in the second part mean grouping?). Run and make sure it does what we expec

Debugging Scenarios

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: