Grep Words From a File

by Vivek Gite [Last updated: November 12, 2008]

Q. How do I grep words from a text file under UNIX / Linux / BSD operating systems?

A. grep is a command line text search utility originally written for Unix / Linux / Windows and Mac OS X. You need to use this tool to grep words from a text file.

Find words from a file

To find word from a file use following syntax:
grep "word" {filename}
Find word Orange in the file called data.txt:
$ grep "orange" data.txt
Grep prints all lines containing 'orange' from the file data.txt, regardless of word boundaries; therefore lines containing 'orangeade' or 'oranges' are also printed.

Grep Words Form the File

To print all lines containing 'orange ' as a word ('orangeade' and 'oranges' will not match):
$ grep -w orange data.txt

Ignore case distinctions with grep

The grep command is case sensitive by default, so this example's output does not include lines containing 'Orange' (with a capital O) unless they also contain 'orange'. To perform ignore case distinctions use the -i option:
$ grep -i orange data.txt
You can combine all options together to find only 'orange ' as a word with no case distinctions:
$ grep -wi orange data.txt

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , ,

Previous post: Install Flash on Linux 64 bit

Next post: HowTo: Find the GNOME Desktop Version