User Tools

Site Tools


using_grep

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
using_grep [2016/09/06 19:22] walkeradminusing_grep [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Using GREP ====== ====== Using GREP ======
 +<color darkorange>2016</color>
 +\\ 
 \\  \\ 
 \\  \\ 
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:x:1000:1000:,,,:/home/pi:/bin/bash<color red>red</color>+    returns: pi:x:1000:1000:,,,:/home/pi:/bin/bash (the two instanced of the word 'pi' will be coloured. 
 +\\  
 +You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option: 
 +\\  
 +    grep -i "pi" /etc/passwd  
 +     
 +    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 "192.168.1.5" /etc/ 
 +     
 +    or 
 +     
 +    grep -R "192.168.1.5" /etc/ 
 +         
 +    example return: /etc/dhcpcd.conf:static ip_address=192.168.1.5/24 
 +\\  
 +\\  
 +===== 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 "boo" file 
 +\\  
 +\\  
 +===== Use grep to search 2 different words ===== 
 +Use the <color Red>egrep</color> command as follows: 
 +\\  
 +    egrep -w 'word1|word2' /path/to/file 
 +\\  
 +\\  
 +===== 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 'word' /path/to/file 
 +\\  
 +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 'bash' /etc/passwd 
 +     
 +    example: 
 +     
 +    1:root:x:0:0:root:/root:/bin/bash 
 +    23:pi:x:1000:1000:,,,:/home/pi:/bin/bash 
 +     
 +    The word 'bash' was found on lines 1 and 23 
 +\\       
 +\\  
 +===== 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 /path/to/file 
 +\\  
 +\\  
 +===== 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:</color> 
 +\\  
 +    dmesg | egrep '(s|h)d[a-z]' 
 +     
 +    [    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: SDHCI platform and OF driver helper 
 +    [    6.716496] bcm2835-wdt 3f100000.watchdog: Broadcom BCM2835 watchdog timer 
 +    [    7.186573] brcmfmac: brcmf_sdio_drivestrengthinit: No SDIO Drive strength init done for chip 43430 rev 1 pmurev 24 
 +\\  
 +Display cpu model name: 
 +\\  
 +    cat /proc/cpuinfo | grep -i 'Model' 
 +     
 +    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 'Model' /proc/cpuinfo 
 +\\  
 +\\  
 +===== How do I list just the names of matching files? ===== 
 +Use the -l option to list file name whose contents mention main(): 
 +\\  
 +    grep -l 'main' *.c 
 +\\  
 +\\  
 +===== Force grep to display output in colours ===== 
 +Finally, you can force grep to display output in colors, enter: 
 +\\  
 +<color red>Raspbian seems to do this by default</color> 
 +\\  
 +    grep --color pi /etc/passwd 
 \\  \\ 
  
 +
 +    
 +    
  
using_grep.1473186176.txt.gz · Last modified: 2023/03/09 22:35 (external edit)