tr (translate characters) in Linux CLI - Time & Space Complexity
We want to understand how the time taken by the tr command changes as the input size grows.
Specifically, how does processing more characters affect execution time?
Analyze the time complexity of the following tr command usage.
cat input.txt | tr 'a-z' 'A-Z'
This command reads text from input.txt and converts all lowercase letters to uppercase.
Look for repeated actions inside the command.
- Primary operation: Reading each character and translating it if it matches.
- How many times: Once for every character in the input file.
As the input file gets bigger, the command processes more characters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character translations |
| 100 | About 100 character translations |
| 1000 | About 1000 character translations |
Pattern observation: The number of operations grows directly with the number of characters.
Time Complexity: O(n)
This means the time taken grows in a straight line with the input size.
[X] Wrong: "The tr command runs instantly no matter the input size."
[OK] Correct: It actually processes each character, so bigger inputs take more time.
Knowing how simple commands like tr scale helps you understand performance in real scripts and pipelines.
"What if we used tr to translate only a fixed small set of characters instead of the whole alphabet? How would the time complexity change?"
Practice
tr command do in Linux?Solution
Step 1: Understand the purpose of
Thetrtrcommand is used to translate or delete characters from input text streams.Step 2: Compare with other options
Options A, C, and D describe other commands likels, compilers, andpwd, which are unrelated totr.Final Answer:
It translates or deletes characters from input text. -> Option AQuick Check:
tr= translate/delete chars [OK]
- Confusing tr with ls or pwd commands
- Thinking tr compiles code
- Assuming tr lists files
tr?Solution
Step 1: Recall
The correct syntax uses single quotes around the sets of characters:trsyntaxtr 'a' 'b'.Step 2: Evaluate options
tr a-z b uses a range which translates all lowercase letters to 'b' (padded), incorrect for only 'a'. Options A and C use invalid syntax fortr.Final Answer:
tr 'a' 'b' -> Option CQuick Check:
Use quotes around sets intr[OK]
- Using range 'a-z' for single character
- Using invalid options like -replace
- Adding extra words like 'to'
echo "hello world" | tr 'a-z' 'A-Z'?Solution
Step 1: Understand the command
The command converts all lowercase letters (a-z) to uppercase (A-Z) in the input text "hello world".Step 2: Apply translation
All letters become uppercase, so "hello world" becomes "HELLO WORLD".Final Answer:
HELLO WORLD -> Option AQuick Check:
Lowercase to uppercase translation = HELLO WORLD [OK]
- Expecting partial uppercase
- Confusing input and output
- Thinking it causes an error
echo "123abc" | tr '0-9' 'a-j'Solution
Step 1: Analyze the command
The command replaces digits 0-9 with letters a-j. Both sets have equal length (10 characters).Step 2: Check for syntax or runtime errors
Quotes are present, ranges are valid and equal length, so no error occurs.Final Answer:
No error, command works correctly -> Option BQuick Check:
Equal length ranges with quotes = valid command [OK]
- Assuming mismatched lengths cause errors here
- Missing quotes around sets
- Expecting output to be empty
input.txt. Which command correctly does this using tr?Solution
Step 1: Understand the goal
The goal is to delete vowels from the input text file.Step 2: Identify correct
Thetroption-doption deletes specified characters. Sotr -d 'aeiou'removes vowels.Step 3: Check other options
tr 'aeiou' < input.txt lacks the second character set, causing an error. Options C and D use invalid options.Final Answer:
tr -d 'aeiou' < input.txt -> Option DQuick Check:
Use-dto delete chars intr[OK]
- Missing second character set operand
- Using invalid options like -r or -remove
- Forgetting to redirect input
Ready to prove it?
Pass the test and this pattern moves unknown
