Quiz: Advanced Text Processing - Sed, Awk, and Pipes
Test your understanding of stream editing, data processing, and command pipelines.
1. What is the pipe operator | used for?
- Creating symbolic links
- Connecting the output of one command to the input of another
- Writing to multiple files simultaneously
- Running commands in parallel
Show Answer
The correct answer is B. The pipe | sends the standard output (stdout) of one command to the standard input (stdin) of another. For example, ls | grep ".txt" lists files and filters for those containing ".txt".
Concept Tested: Pipe Operator
See: Chapter 9 - Pipes
2. What does sed 's/old/new/g' file.txt do?
- Deletes all lines containing "old"
- Replaces all occurrences of "old" with "new"
- Searches for "old" and prints line numbers
- Sorts the file alphabetically
Show Answer
The correct answer is B. The sed substitution command s/old/new/g replaces all occurrences of "old" with "new". The g flag means "global" (all occurrences per line), without it only the first occurrence on each line is replaced.
Concept Tested: Sed Command, Text Substitution
3. What does > do in a command like ls > files.txt?
- Appends output to the file
- Redirects output to the file, overwriting it
- Reads input from the file
- Compares the output to the file
Show Answer
The correct answer is B. The > operator redirects standard output to a file, overwriting any existing content. Use >> to append instead of overwriting. For example, ls > files.txt saves the directory listing to files.txt.
Concept Tested: Output Redirection
4. What does awk '{print $1}' file.txt do?
- Prints the first line of the file
- Prints the first field (column) of each line
- Prints only one copy of duplicate lines
- Deletes the first column
Show Answer
The correct answer is B. In awk, $1 refers to the first field (column) of each line, separated by whitespace by default. So awk '{print $1}' extracts and prints just the first word/column from each line.
Concept Tested: Awk Command
5. What does 2> redirect?
- Standard output (stdout)
- Standard error (stderr)
- Standard input (stdin)
- The second line of output
Show Answer
The correct answer is B. File descriptor 2 is standard error. 2> redirects error messages to a file. For example, command 2> errors.txt saves error messages to errors.txt while normal output still goes to the terminal.
Concept Tested: Standard Error, Redirection
6. What does the sort command do by default?
- Sorts numerically
- Sorts in reverse order
- Sorts lines alphabetically
- Removes duplicates
Show Answer
The correct answer is C. By default, sort sorts lines in alphabetical (lexicographic) order. Use -n for numerical sorting, -r for reverse order, and -u to also remove duplicates.
Concept Tested: Sort Command
7. What does uniq do and what's its requirement?
- Makes files unique by adding timestamps
- Removes duplicate adjacent lines (input must be sorted)
- Finds unique files in a directory
- Creates unique filenames
Show Answer
The correct answer is B. uniq removes duplicate adjacent lines. Since it only compares neighboring lines, you typically need to sort input first: sort file.txt | uniq. Use -c to count occurrences.
Concept Tested: Uniq Command
8. What does cat file.txt | tr 'a-z' 'A-Z' do?
- Deletes lowercase letters
- Translates lowercase letters to uppercase
- Counts lowercase letters
- Transposes rows and columns
Show Answer
The correct answer is B. The tr (translate) command replaces characters. Here it maps each lowercase letter (a-z) to its uppercase equivalent (A-Z), effectively converting the entire file to uppercase.
Concept Tested: Tr Command
9. What does &> do in bash?
- Runs command in background
- Redirects both stdout and stderr to a file
- Creates an alias
- Appends to a file
Show Answer
The correct answer is B. The &> operator redirects both standard output (stdout) and standard error (stderr) to the same file. It's equivalent to > file 2>&1. For example, command &> output.txt captures all output.
Concept Tested: Redirection
10. What does cut -d',' -f2 data.csv do?
- Cuts the file into two pieces
- Extracts the second comma-separated field from each line
- Removes the second line
- Deletes commas from the file
Show Answer
The correct answer is B. The cut command extracts portions of text. Here -d',' sets the delimiter to comma, and -f2 selects the second field. This is perfect for extracting columns from CSV files.
Concept Tested: Cut Command