
Grep scans its input for a pattern, and can display the selected pattern, the line numbers of the filenames where the pattern occurs, The command uses the following syntax
grep options pattern filesname(s)
grep searches for pattern in one or more filenames.
Example for Grep command:
- grep âsalesâ emp.lst
- grep âdirectorâ emp1.lst emp2.lst
- grep âjai sharmaâ emp.lst
- grep âjai Sharma $varâ emp.lst
—————-Grep options—————————
Ignoring Case (i) When you look for a name, but you are not sure of the case, grep offers the âi (ignore) option which ignores case for patteen matching.
> Grep âi âagarwalâ emp.lst
Deleting Lines or Inverse(-v): -v (inverse) option selects all except lines containing the pattern. Thus, you can create a file other list containing all but director.
> Grep âv âdirectorâ emp.lst > other list
Displaying line Numbers (-n): The ân(number) options displays the line numbers containing the pattern, along with the lines:
> grep ân âmarketingâ emp.lst
Counting Line Containing patterns (-c): The âc (count) option counts the number of lines containing the pattern ( which is not the same as number of occurrences).
- grep âc director emp.lst
- grep âc director emp*.lst
Displaying Filenames (-l): The âl (list) option displays only the files names of files containing the pattern.
> grep âl âmanagerâ *.lst
Matching Multiple Patterns(-e): With the âe option, you can match the three agarwals by using grep like this:
> grep âe âAgarwalâ âe âaggarwalâ âe âagarwalâ emp.lst
Taking patterns from a file (-f): we can place all patterns in a separate file, one pattern per lin. Grep takes inputs from there with the âf option:
> grep âf pattern.lst emp.lst
Â
- Use of runtime variables to save into another variable using register in Ansible - September 6, 2018
- Ansible & Ansible Tower Variable Precedence Hierarchy - September 6, 2018
- How to use template in Ansible? - September 6, 2018