using_grep
This is an old revision of the document!
Table of Contents
Using GREP
Grep is a super useful tool, but it is difficult to master, so in this section we are going to explore some examples of the use of GREP. Grep can be as complex as your imagination can muster, but some very simple examples of Grep use will get you started nicely.
The name, “grep”, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p
grep command syntax
The syntax is as follows:
grep 'word' filename grep 'word' file1 file2 file3 grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something' command option1 | grep 'data' grep --color 'data' filename
How do I use grep command to search a file?
Search /etc/passwd file for boo user, enter:
grep pi /etc/passwd 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
using_grep.1473186546.txt.gz · Last modified: 2023/03/09 22:35 (external edit)