fgrep
The fgrep command is similar to grep, but with three main differences: You can use it to search for several targets at once, it does not allow you to use regular expressions to search for patterns, and it is faster than grep. When you need to search many files or a very large file, the difference in speed can be significant.
With fgrep, you can search for lines containing any one of several targets. For example, the following command finds all entries in the phone numbers ftc file that contain any of the words “saul”, “michelle”, or “Walter“:
$ fgrep "saul
> michelle
> walter" ftc.txt
OUTPUT
walter White 555–2222
saul (home) 555–1145
michelle 555–3244
When you give fgrep multiple search targets, each one must be on a separate line, and the entire search string must be in quotation marks. In this example, if you didn’t put michelle on a separate line you would be searching for saul michelle, and if you left out the quotes, the command would execute as soon as you hit ENTER.
With the −f (file) option, you can tell fgrep to take the search targets from a file, rather than having to enter them directly If you had a file in your home directory named .friends containing the usernames of your friends on the system, you could use fgrep to search the output of the finger command for the names on your list, like this:
$ finger | fgrep −f −/.friends
ILLUSTRATION OF THE CODE IN LINUX TERMINAL
egrep
The egrep command is the most powerful member of the grep command family You can use it like fgrep to search for multiple targets, and it provides a larger set of regular expressions than grep.
In fact, if you find yourself using the extended features of egrep often, you may want to add an alias that replaces grep with egrep in your shell configuration file. (For example, if you are using bash, you could add the line “alias grep=egrep” to your .bashrc .)
You can tell egrep to search for several targets in two ways: by putting them on separate lines as in fgrep, or by separating them with the vertical bar or pipe symbol ( | ). For example, the following command uses the pipe symbol to tell egrep to search for the words dan, robin, ben, and walter in the file ftc.txt (phonelist) :
$ egrep "dan|robin|ben|walter" ftc.txt
OUTPUT
walter White 555–2222
dan dnidz x1234
robin rpelc x3141
ben bsquared x9876
Note that there are no spaces between the pipe symbol and the targets. If there were, egrep would consider the spaces part of the target string. Also note the use of quotation marks to prevent the shell from interpreting the pipe symbol as an instruction to create a pipeline.
ILLUSTRATION OF THE CODE IN LINUX TERMINAL
Additional egrep Regular Expressions
Symbol | Definition | Example | Matches |
---|---|---|---|
+ | Matches one or more repetitions of the previous item. | .+ | any non-empty line |
? | Matches the previous item zero or one times. | index\.html? | index.htm, index.html |
( ) | Groups a portion of the pattern. | script(\.pl)? | script, script.pl |
| | Matches either the value before or after the |. | (E|e)xit | Exit, exit |
The egrep command provides most of the basic options of both grep and fgrep.
You can tell it to ignore uppercase and lowercase distinctions (−i),
search recursively through subdirectories (−r),
print the line number of each match (−n),
print only the names of files containing target lines (−l),
print lines that do not contain the target (−v),
and take the list of targets from a file (−f).
Addition Regular Expressions for references
Symbol | Definition | Example | Matches | |||
---|---|---|---|---|---|---|
. | Matches any single character. | th.nk | think, thank, thunk,etc. | |||
\ | Quotes the following character. | script\.py | script.py | |||
* | Previous item may occur zero or more times in a row. | ap*le | ale, apple, etc. | |||
[a-z] | Matches any one of the characters in the range. | [0–9]* | any number: 0110, 27, 9876, etc. | |||
$ | Anchor the pattern to the end of the string. | \.$ | any string ending in a period | |||
^ | Anchor the pattern to the beginning of a string. | ^ If | any string beginning with If | |||
{n,m} | Previous item must occur at least n times but no more than m times. | \*{3,5} | ***, ****,***** | |||
[ ] | Matches any one of the characters inside. Frequently used with ranges. | [QqXx]* | Q, q, X, or x | |||
[^] | Matches any character not inside the brackets. | [^AZaz] | any nonalphabetic character, such as 2 |
Morae Q!
- Convert from binary number to decimal number.
- Swap two numbers by using division and multiplication.
- Compute foot and inches into centimetres.
- Concatenate Strings.
- Convert lowercase letters to uppercase letters.
- Compute the sum of all the digits of N.
- Compute the area of the pentagon.
- Get input using scanner.
- Find the distance between two points (x1,y1) and (x2,y2).
- Finding patterns in a file using frep and egrep .
- Detecting a cycle in a directed graph using Depth First Traversal.
- Topological ordering in a Graph.
- Storing graphs using adjacency list.
- Introduction to graph theory.
- Graph Adjacency Matrix pseudo code.
- Adjacency Matrix for storing graphs.
- Dijkstra’s shortest path algorithm .
- Breadth first search (BFS) Algorithm.
- Find the multiple of given number in the list of integers.
- Finding patterns in files using grep .