We all use grep command when we want to search for patterns of text in any file(s).
ED (Editor) is a text editor that was developed in the mid-1960s by Ken Thompson and other early pioneers of computing. It is one of the oldest text editors still in existence and has a long history with Unix-based systems.
The first versions of Unix used ED as the default text editor. To use ED, users interact with the editor by entering specific commands to perform various tasks, such as inserting, deleting, or searching for text.
For example, to add text, users would use the “a” command, followed by the text they want to insert, and then end the insertion with a dot on a new line.
In a quest to know who the authors of the “Federalist: A Collection of Essays, written in Favour of the New Constitution” one of the fundamental American documents that was published anonymously under the name “PHILO-PUBLUS“, Lee Edward McMahon wanted to figure out who wrote it.
It was around 1 MB which was not possible to edit on ED editor. He wanted to search for an occurrence of a particular word in that book. He sent that to Ken Thompson hoping to get some help from him. Ken Thompson wrote a program overnight which is popularly now called GREP.
In ED editor, the command ‘g‘ indicates global and ‘p‘ indicates print. So, as per ED, globally search for a regular expression and print it.
Global Regular Expression Print –> g/re/p –> GREP.
That’s how it began and grep is what it is now.
Some examples of how to use grep
grep “someword” somefile.txt
It prints out the statement where that ‘someword’ occurs in ‘somefile’.
grep -i “some” anotherfile.txt
It ignores the case(Upper and lower) while searching and prints both some and Some.
grep -r “searching” adirectory
It recursively searches for “searching” in al files within “adirectory” directory and its subdirectories.
grep -v “notthis” somefile.txt
It inverts the match and displays lines that don’t contain the word “notthis”.
grep -c “counting” somefile.txt
It counts the numberof occurences of the word “counting” in the file and displays the count.
grep “^this” someregfile.txt
‘^’ in the above regex indicates the start of a line, so grep prints all the lines that start with ‘this’.
There are plenty more, these are just a few.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
