- The use of grep
The grep’s syntax goes like this
grep [options] regularexp [files].
It is a powerful tool used to filter a file for specific matches dictated by the regular expression that has been supplied in the regularexp field of its syntax.
Option examples to be used. The following is a partial list of the commonly used options with grep. More are detailed in its man pages.
-f file or --file=file: Take the pattern from a file rather than from the command line
-i or --ignore-case: tells it not to consider cases during the search
-r or --recursive: tells the program to search within a folder together with its sub-directories.
-c or --count:tells grep to return the number of lines that contain the matched expression.
-E or --extended-regexp:this option tells the program to use extended regular expressions.
To find a file with the interface eno1 or eno2 within the /etc directory alone, do:
#grep eno /etc/*
To find a file with the interface eno1 or eno2 within the /etc directory together with its sub-directories, do:
#grep -r eno /etc/*
To find files in your computer that have the host name of computingforgeeks.com or computing.org and assuming you want the one that has an assigned IP address beginning with 192. You do:
#grep -E “(computingforgeeks.com|computing.org).*192” /etc/*
grep can as well be used with a number of other programs such as cat or ps and many more. For instance:
#cat /etc/hostname | grep 192
#ps aux | grep ssh
sed is a powerful tool that is used to change the contents of a file and thereafter print them on standard output. This means that the changes being implemented are temporary unless they are saved in any of the ways described earlier.
The syntax of sed goes like this:
#sed [options] commands [file] or #sed [options] -f commands [file]
Command Explanation = Used to display the current line number. i\text An insertion tool to Insert text into the file. w filename It Writes the current pattern space to the file specified. c\text Replace the selected range of lines with the provided text. a\text Add the text(append) text to the file. q Immediately quit the script, but print the current pattern space Q quit the script. s/regexp/replacement Replace text that matches the regular expression (regexp) with
r filename Append text from filename into the file.
As an appreciation for the content we put out,
we would be thrilled if you support us!
As we continue to grow, we would wish to reach and impact more people who visit and take advantage of the guides we have on our blog. This is a big task for us and we are so far extremely grateful for the kind people who have shown amazing support for our work over the time we have been online.
Thank You for your support as we work to give you the best of guides and articles. Click below to buy us a coffee.