tr for character transformation in Bash Scripting - 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 code snippet.
echo "hello world" | tr 'a-z' 'A-Z'
This code converts all lowercase letters in the input string to uppercase letters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
trcommand reads each character from the input and transforms it if it matches the first set. - How many times: It processes each character exactly once, so the number of operations grows with the number of characters.
As the input string gets longer, tr must check and possibly transform each character.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks and transformations |
| 100 | About 100 character checks and transformations |
| 1000 | About 1000 character checks and transformations |
Pattern observation: The work grows directly with the number of characters; doubling input doubles work.
Time Complexity: O(n)
This means the time to transform characters grows linearly with the input size.
[X] Wrong: "The tr command runs in constant time no matter the input size."
[OK] Correct: Since tr processes each character, more characters mean more work, so time grows with input length.
Understanding how simple commands like tr scale helps you reason about script performance and efficiency in real tasks.
"What if we used tr to replace multiple characters with a single character? How would the time complexity change?"
Practice
tr command do in bash scripting?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 B, C, and D describe unrelated commands or functions in bash.Final Answer:
It transforms or deletes characters from input text streams. -> Option AQuick Check:
tr= transform/delete characters [OK]
- Confusing tr with file listing commands like ls
- Thinking tr compiles scripts
- Assuming tr runs commands in background
tr?Solution
Step 1: Identify the correct character ranges for case conversion
To convert lowercase to uppercase, use'a-z'as source and'A-Z'as target.Step 2: Check the syntax correctness
echo "hello" | tr 'a-z' 'A-Z' correctly usestr 'a-z' 'A-Z'. Other options either reverse case or use wrong ranges.Final Answer:
echo "hello" | tr 'a-z' 'A-Z' -> Option BQuick Check:
Lowercase to uppercase = echo "hello" | tr 'a-z' 'A-Z' [OK]
- Swapping source and target ranges
- Using digits instead of letters
- Mixing uppercase and lowercase incorrectly
echo "Hello World 123" | tr -d '0-9'
Solution
Step 1: Understand the
The-doption intr-doption deletes characters matching the set given, here digits '0-9'.Step 2: Apply deletion to the input string
All digits '1', '2', '3' are removed, leaving "Hello World " with spaces intact.Final Answer:
Hello World -> Option DQuick Check:
Digits deleted = "Hello World " [OK]
- Expecting digits to be replaced, not deleted
- Removing spaces accidentally
- Confusing -d with character translation
echo "abc123" | tr 'a-z' 'A-Z' -d '0-9'
Solution
Step 1: Understand
troption usagetrdoes not support combining translation and deletion in one command; they are separate operations.Step 2: Analyze the command
The command tries to translate and delete simultaneously, which is invalid syntax.Final Answer:
You cannot combine translation and deletion in one tr command. -> Option CQuick Check:
Translation + deletion together = invalid [OK]
- Trying to chain translate and delete in one tr call
- Assuming option order fixes the problem
- Expecting partial command success
Solution
Step 1: Understand character replacement with
To replace vowels with '*', map all vowels to a single character '*'.trStep 2: Analyze each option
echo "banana" | tr 'aeiou' '*' replaces each vowel with '*'. echo "banana" | tr 'aeiou' 'AEIOU' changes vowels to uppercase vowels. echo "banana" | tr -d 'aeiou' deletes vowels. echo "banana" | tr 'aeiou' 'ABCDE' replaces vowels with A,B,C,D,E respectively.Final Answer:
echo "banana" | tr 'aeiou' '*' -> Option AQuick Check:
Vowels replaced by '*' = echo "banana" | tr 'aeiou' '*' [OK]
- Using a target set like 'ABCDE' instead of '*'
- Deleting vowels instead of replacing
- Replacing vowels with uppercase instead of '*'
Ready to prove it?
Pass the test and this pattern moves unknown
