Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Command

Description

grep

Code Block
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c
...
 -i, --ignore-case         ignore case distinctions
...
 -n, --line-number         print line number with output lines
...
 -r, --recursive           like --directories=recurse
...
# grep is case-sensitive

...

Examples: Search a file

Info

Remember, Linux in general, and grep are case-sensitive.

Info

Search for the capitalized NVIDIA text.

Code Block
[intro_to_linux]$ grep nvidia software.csv
[intro_to_linux]$
# Remember: grep is case-sensitive
[intro_to_linux]$ grep NVIDIA software.csv
libraries and toolkits,cuDNN,cudnn,beartooth,The NVIDIA CUDA Deep...
libraries and toolkits,TensorRT,,beartooth,"NVIDIA TensorRT, an...
# Some items returned, but there could be others.
# Neither of the above picked up “Nvidia”.
Info

The above identified and returned two locations where NVIDIA was found.

But this would not pick up the text Nvidia.

Modify the command-line to ignore-case using the -i option.

Code Block
[intro_to_linux]$ grep -i NVidia software.csv
compiler,NVidia HPC SDK,nvhpc,"beartooth,teton"...
libraries and toolkits,cuDNN,cudnn,beartooth,The NVIDIA CUDA Deep...
libraries and toolkits,TensorRT,,beartooth,"NVIDIA TensorRT, an...
# Ignore the case of the word to search for.
Info

Modify the command-line to display the the line numbers within the file using the -n option.

Code Block
[intro_to_linux]$ grep -n -i NVidia software.csv
145:compiler,NVidia HPC SDK,nvhpc,"beartooth,teton"...
152:libraries and toolkits,cuDNN,cudnn,beartooth,The NVIDIA CUDA Deep...
166:libraries and toolkits,TensorRT,,beartooth,"NVIDIA TensorRT, an...
# Include the line # from which the text was found.

...

Examples: Search folders and files

Info

Use the wildcard * to search all files within the folder.

Code Block
[intro_to_linux]$ cd clusters/
[clusters]$ grep -i nvidia *
beartooth.html:    .../788758554/NVidia+HPC+SDK">NVidia HPC SDK</a></td>
teton.html:    .../788758554/NVidia+HPC+SDK">NVidia HPC SDK</a></td>
[clusters]$ cd ..
Info

Remember: case-sensitivity:

Code Block
[intro_to_linux]$ grep -i nvidia *
grep: clusters: Is a directory
software.csv:compiler,NVidia HPC SDK,nvhpc,"beartooth,teton"...
software.csv:libraries and toolkits,cuDNN,cudnn,beartooth,The NVIDIA CUDA Deep...
software.csv:libraries and toolkits,TensorRT,,beartooth,"NVIDIA TensorRT, an....
Info

Modify the command-line, using the -r option, to recursively search through folders within this folder.

Code Block
[intro_to_linux]$ grep -r -i nvidia *
clusters/teton.html:    .../788758554/NVidia+HPC+SDK">NVidia HPC SDK</a></td>
clusters/beartooth.html:    .../788758554/NVidia+HPC+SDK">NVidia HPC SDK</a></td>
software.csv:compiler,NVidia HPC SDK,nvhpc,"beartooth,teton"...
software.csv:libraries and toolkits,cuDNN,cudnn,beartooth,The NVIDIA CUDA Deep...
software.csv:libraries and toolkits,TensorRT,,beartooth,"NVIDIA TensorRT, an...

...

  1. Which named applications are related to the words “bayes”?

  2. Which files contain reference to IPA?

...

Answers

Info

Question 01: Which named applications are related to the words “bayes”?

This assumes we want to find all instances of “bayes” regardless of case.

Code Block
[intro_to_linux]$ grep -i bayes software.csv
application,Bayescan,bayescan,beartooth,"BayeScan aims...
application,Beast1,beast1,wildiris,BEAST is a cross-platform program for Bayesian...
application,Beast2,beast2,beartooth,"BEAST 2 is a cross-platform program for Bayesian...
application,Freebayes,freebayes,beartooth,"freebayes is a Bayesian genetic...
application,Jags,jags,"beartooth,teton",Just Another Gibbs Sampler. It is a program for analysis of Bayesian hierarchical...
application,RevBayes,revbayes,wildiris,Bayesian phylogenetic...
application,ROHan,rohan,teton,"ROHan is a Bayesian framework...
application,SourceTracker2,sourcetracker2,"beartooth,teton","SourceTracker, a Bayesian approach...
Info

Question 02: Which files contain reference to IPA?

In this case we want to explicitly search for IPA (all upper-case) and recursively search through all folders.

Code Block
[intro_to_linux]$ grep -r IPA *
clusters/beartooth.html:    .../pages/1893597185/IPA">IPA</a></td>
software.csv:application,IPA,ipa,beartooth,Improved Phased Assembler (IPA) is...

...