Week 5- Researching Commands

Researching commands for grep.

Anh Pham
Anh Pham @phuanh004

In this lab, I research commands for grep, a tool that helps you to find and replace text in files. It is a tool that is very useful for developers. It is also a tool that is very useful for me because I often need to find and replace text in files. Since the technical folder has a lot of text files (in *.txt), I will research commands for grep for the lab.

The folder technical of this lab can be found here.

1. Word Regular Expression (-w)

-w or --word-regexp is used to match the whole word could be used with the regular expression (regex). I think this is the most useful flag for me since it not only to find the word but also using regex to find several words that have the same pattern or special cases, e.g. digits or punctuations.

Find words that match

grep -w "<keyword>" <file>

# -- or --
grep --word-regexp "<keyword>" <file>


# E.g. Find words that match with `Programming`
# in `technical/government/Gen_Account_Office/og96037.txt`
grep -w "Programming" technical/government/Gen_Account_Office/og96037.txt

Word Regular Expression

Find digits in a text file

grep -w "^[[:digit:]]" <file>

# E.g. Find words that match with the digit type
# in `technical/government/Post_Rate_Comm/Mitchell_6-17-Mit.txt`
grep -w "^[[:digit:]]" technical/government/Post_Rate_Comm/Mitchell_6-17-Mit.txt

Find digits in a text file

Find punctuation characters in a text file

grep -w "^[[:punct:]]" <file>

# E.g. Find words that match with the punctuation
# in `technical/government/Post_Rate_Comm/Mitchell_6-17-Mit.txt`
grep -w "^[[:punct:]]" technical/government/Post_Rate_Comm/Mitchell_6-17-Mit.txt

Find words with punch in a text file

2. Line Number (-n)

-n or --line-number prints the line number of each match. This is helpful because you can see the line number of the line of the matches and then find and work with that easily in the text editor later.

Note: The line number counter is reset for each file processed. This option is ignored if -c, -L, -l, or -q is specified.

Line number in one file

grep -n "<keyword>" <file>

# -- or --
grep --line-number "<keyword>" <file>

# E.g. Find line number of the line that contains the word `Bye`
# in `technical/911report/chapter-1.txt`
grep -n "Bye" technical/911report/chapter-1.txt

Line number in one file

Line number in lines contain the keyword

# E.g. Print the line number of each line that contains
# the word `Programming` in subfolder `technical/government/`
grep -w "Programming" --line-number  technical/government/*/*.txt

Print line number of each line in a file that contains the word programming

Line number with digits

grep -n "<keyword>" <file>

# -- or --
grep --line-number "<keyword>" <file>

# E.g. Print the line number of each line that has digit in it
grep -w -n "^[[:digit:]]" technical/government/Post_Rate_Comm/Mitchell_6-17-Mit.txt

Line number with digits

3. Ignore Case Sensitivity (-i)

-i or --ignore-case match the word with case insensitive. This is useful when you want to find the word with different cases. For example, if you want to find the word programming and Programming, because you do not know if it is located in the beginning or in the end of a sentence, you can use this flag.

Case insensitive on one word

grep -i "<keyword>" <file>

# -- or --
grep --ignore-case "<keyword>" <file>

# Ignore case sensitivity (-i) when searching for the word `programming`
# & print the line number (-n) and has the word `programming` (-w).
# Could be written in short (-win).
grep -win "programming" <file>

Print the line number of each line in a file that contains the word programming in one file

Case insensitive on multiple files

# Ignore case sensitivity (-i) when searching for the word `programming`
# & print the line number (-n) and has the word `programming` (-w).
# Could be written in short (-win).
grep -win "programming" technical/government/*/*.txt

Ignore case sensitivity when searching for the word programming

Using grep with find

# Find the first file in the `technical` directory that contain
# the word `programming` using `xargs`.
find technical -type f -name "*.txt" | xargs grep -i "Programing"

Find the first file

Categories

Development