Searching text files using REGEX (3) Flashcards
Objective 103.7 Weight 3
Which of the following commands, would you use to display every B in the file as an A, if the file is named test.txt?
- tr A B > test.txt
- tr B A > test.txt
- tr A B test.txt
- tr B A test.txt
- tr A B < test.txt
- tr B A < test.txt
- tr B A < test.txt
The tr command that displays every B in the file as an A, if the file is named test.txt is: tr B A < test.txt. The first character (B in this case) after tr is the character for which to search. The second character (A in this case) is the letter to replace any found characters (B in this case). The < is needed to redirect STDIN so that the designated file is read into the command.
You want to find only the text words fin, fun, and fan in a text file named test.txt. Which of the following commands will accomplish this task?
1. grep f*n test.txt
2. grep fin test.txt; grep fun test.txt; grep fan test.txt
3. grep f?n test.txt
4. grep f/n test.txt
5. grep fun; fin; fan test.txt
6. grep f[aiu]n test.txt
- grep fin test.txt; grep fun test.txt; grep fan test.txt
AND - grep f[aiu]n test.txt
The most efficient method for finding the words fin, fun, and fan in a text file named test.txt is by using the [aiu] bracketed wildcard in the search pattern, so that the full command is fin, fun, and fan in a text file named test.txt. However this command, using a semi-colon (;) to issue multiple commands on the same line, will work as well: grep fin test.txt; grep fun test.txt; grep fan test.txt. The fn and f?n search patterns do not work, because they will potentially find more than just the words fin, fun, and fan.
You decide to search for any text word starting with the letter s and ending with the letter n using a grep command on the text file test.txt. What type of expression will you use?
- BRE
- ERE
- regex (7)
- seven-regex
- basic regular expression
- extended regular expression
- BRE
AND - basic regular expression
To search for any text word starting with the letter s and ending with the letter n using a grep command on the text file test.txt, you would use the command: grep s*n test.txt. The s*n pattern is a basic regular expression or BRE. ERE refers to extended regular expressions, and regex (7), which is actually a reference to the man pages on regular expressions, refers to both EREs and BREs, so it is too general of an answer to be correct. There is no such thing as seven-regex.