Everything you need to know about the Linux UBUNTU Grep Command

The Grep Command is one of the tools that is currently available on computers, and with which you can carry out different activities. For this reason, today we leave you everything you need to know about the Linux UBUNTU Grep Command.

What is the Linux Grep command?

The first thing to note is that Grep means Globally Search For Regular Expression and Print out, and it is one of the most used command tools in the world to search the text strings, and find matches. Although, many users also use it to get words or their combination in a file.

For its operation it needs two basic parameters: the pattern that you are going to search for and the file in which you are going to carry out the search. 

An important fact that you should also take into account is to know the regular expressions. Since, later you will be able to create or enter different combinations so that the search can improve. 

Situational Examples Using the Grep Command

There are many situational examples in which you can use this command, here are a few:

Find a word in a text file

  • If you want to use this command, to search for a word, you must type ''grep file search''. 
  • Substituting the word in question where it says search, and in file the name of the one you are looking for.

Find a word regardless of case

In this case all you have to do is add ''-Yo'', then it would be like ''grep -i file search''. 

Count of words that match the search 

Also with this command you can know the times in which a word is used in a text file. You just have to add -c, and it would be ''grep -c file search''.  

Search multiple keywords

The previous examples make it clear that it is a command that is available to search for a single word, but you can also perform many queries using that single command. For this, you must place ''grep search1 file'', ''grep search2 file''.

It is very simple to use, you must place the first search, and then add the second command.

Find a word in multiple files at once

Another option you have is to search in different files with the same command. This is: ''grep -l search_word ./*''. The terminal displays all the files that have the word you've been looking for.

Search for files with the grep command

In this case, the command that you must place is: grep option/s 'text string' file_where_to search.

Access system information with the grep command

The grep command is not only available to search for different keywords, you can also find information within the system. What you should write on your computer is the following: ''grep -i 'Model' /proc/cpuinfo''.

Find lines that do not contain a keyword (reverse lookup)

Search for lines within the file that do not have a keyword, nor the string of characters ''the'', it is also possible with the grep command, you just have to reverse the whole process and add -v and ready. would remain, ''grep -v -F the. Bashrc.''

basic regular expressions grep command

Although it is true, there are many expressions that are used with the grep command, however, there are some that are considered basic, and these are the following:

  • -E: This function allows you to identify a search regular expression.
  • -Colour: It is in charge of highlighting the color of all the text strings that are similar to the search, or that match.
  • -H: It shows you the name of the file where you want to search for a word, or a text string.
  • .*: With this you can represent multiple characters.
  • .: Represents a single character.
  • [a e i o u]: These letters are used to represent some characters.
  • [Az]: Allows you to present a range of characters.
  • ^: It represents the moment in which you are starting a line. 
  • $: They show when a line is ending. 

extended regular expressions grep command

Extended Regular Expressions are also known as ERE due to its acronym Extended Regular Expressions, they help you create more complex patterns. 

  • |: You can specify two words or a set of characters, known as a pattern. 
  • (): These are used when you want to designate some additional regular expressions, or subexpressions. 

It is important to note that for the grep command to recognize these symbols it is important to use the -E switch, or you can reduce it to ''egrep''.

Leave a comment