using_grep
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
using_grep [2016/09/06 19:19] – walkeradmin | using_grep [2023/03/09 22:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using GREP ====== | ====== Using GREP ====== | ||
+ | <color darkorange> | ||
+ | \\ | ||
\\ | \\ | ||
\\ | \\ | ||
Line 23: | Line 25: | ||
Search /etc/passwd file for boo user, enter: | Search /etc/passwd file for boo user, enter: | ||
\\ | \\ | ||
- | grep boo /etc/passwd | + | grep pi /etc/passwd |
+ | |||
+ | returns: pi: | ||
\\ | \\ | ||
+ | You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option: | ||
+ | \\ | ||
+ | grep -i " | ||
+ | | ||
+ | This will return the word pi, PI, pI or Pi (all case combinations are allowed) | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Use grep recursively ===== | ||
+ | You can search recursively i.e. read all files under each directory for a string “192.168.1.5” | ||
+ | \\ | ||
+ | grep -r " | ||
+ | | ||
+ | or | ||
+ | | ||
+ | grep -R " | ||
+ | | ||
+ | example return: / | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Use grep to search words only ===== | ||
+ | When you search for boo, grep will match fooboo, boo123, barfoo35 and more. You can force the grep command to select only those lines containing matches that form whole words i.e. match only boo word: | ||
+ | \\ | ||
+ | grep -w " | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Use grep to search 2 different words ===== | ||
+ | Use the <color Red> | ||
+ | \\ | ||
+ | egrep -w ' | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Count line when words has been matched ===== | ||
+ | The grep can report the number of times that the pattern has been matched for each file using -c (count) option: | ||
+ | \\ | ||
+ | grep -c ' | ||
+ | \\ | ||
+ | Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained: | ||
+ | \\ | ||
+ | grep -n ' | ||
+ | | ||
+ | example: | ||
+ | | ||
+ | 1: | ||
+ | 23: | ||
+ | | ||
+ | The word ' | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Grep invert match ===== | ||
+ | You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: | ||
+ | \\ | ||
+ | grep -v bar / | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== UNIX / Linux pipes and grep command ===== | ||
+ | grep command often used with shell pipes. In this example, <color red>show the name of the hard disk devices:</ | ||
+ | \\ | ||
+ | dmesg | egrep ' | ||
+ | | ||
+ | [ 2.535530] sdhci: Secure Digital Host Controller Interface driver | ||
+ | [ 2.539697] sdhci: Copyright(c) Pierre Ossman | ||
+ | [ 2.542163] sdhost: log_buf @ bac46000 (fac46000) | ||
+ | [ 2.619698] mmc0: sdhost-bcm2835 loaded - DMA enabled (>1) | ||
+ | [ 2.691142] sdhci-pltfm: | ||
+ | [ 6.716496] bcm2835-wdt 3f100000.watchdog: | ||
+ | [ 7.186573] brcmfmac: brcmf_sdio_drivestrengthinit: | ||
+ | \\ | ||
+ | Display cpu model name: | ||
+ | \\ | ||
+ | cat / | ||
+ | | ||
+ | model name : ARMv7 Processor rev 4 (v7l) | ||
+ | model name : ARMv7 Processor rev 4 (v7l) | ||
+ | model name : ARMv7 Processor rev 4 (v7l) | ||
+ | model name : ARMv7 Processor rev 4 (v7l) | ||
+ | \\ | ||
+ | However, above command can be also used as follows without shell pipe: | ||
+ | \\ | ||
+ | grep -i ' | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== How do I list just the names of matching files? ===== | ||
+ | Use the -l option to list file name whose contents mention main(): | ||
+ | \\ | ||
+ | grep -l ' | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Force grep to display output in colours ===== | ||
+ | Finally, you can force grep to display output in colors, enter: | ||
+ | \\ | ||
+ | <color red> | ||
+ | \\ | ||
+ | grep --color pi / | ||
+ | \\ | ||
+ | |||
+ | | ||
+ | | ||
using_grep.1473185994.txt.gz · Last modified: 2023/03/09 22:35 (external edit)