Searching text files using REGEX (3) Flashcards

Objective 103.7 Weight 3

1
Q

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?

  1. tr A B > test.txt
  2. tr B A > test.txt
  3. tr A B test.txt
  4. tr B A test.txt
  5. tr A B < test.txt
  6. tr B A < test.txt
A
  1. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A
  1. grep fin test.txt; grep fun test.txt; grep fan test.txt
    AND
  2. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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?

  1. BRE
  2. ERE
  3. regex (7)
  4. seven-regex
  5. basic regular expression
  6. extended regular expression
A
  1. BRE
    AND
  2. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly