Skip to main content

Posts

Showing posts with the label grep

Display uncommented lines of a file

As you must be aware that in any configuration file under Linux OS, there is hundreds of lines, most of them are commented with explanations. Therefore going though such files takes time to locate the line we are looking for. I use the command below to remove all the commented lines and blank lines by combining two commands using pipe '|'. In my case the comments are marked with #. grep -v "^#" main.cf | grep -v "^$" The grep -v "^#" filename will remove the commented line. The grep -v "^$" will remove the blank lines.

Use grep to search file

Search /etc/passwd for boo user: grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: grep -i "boo" /etc/passwd Use grep recursively You can search recursively i.e. read all files under each directory for a string "boo": grep -r "boo" /etc/ You can search for a string boo irrespective of word case and recursive: grep -ir "boo" /etc/ When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word: grep -w "boo" /etc/ Grep invert match You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: grep -v boo /etc/