#155 Search string in the text using sed

sed 'p' /usr/share/dict/words

The p operator will print the matched pattern. In this case, we have not specified a pattern
so we will match everything. Printing the matched lines without suppressing STDOUT will
duplicate lines. The result of this operation is to print all the lines in the “words” file twice.
To suppress STDOUT, we use the -n option.

sed -n 'p' /usr/share/dict/words

We can specifically work with just a range of lines. For example, from line 1 to 20.

sed -n '1,20 p' /usr/share/dict/words

Now using regular expression, we will search for lines started with “root”:

sed -n '/^root/  p' /usr/share/dict/words

Output:

root
root's
rooted
rooter
rooting
rootless
roots

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading