cut and paste in Bash Scripting - Time & Space Complexity
When using cut and paste commands in bash, it's important to know how the time to run the commands changes as the input size grows.
We want to understand how the script's speed changes when working with bigger files or more lines.
Analyze the time complexity of the following code snippet.
#!/bin/bash
input_file="data.txt"
output_file="result.txt"
# Cut first column and paste it to output file
cut -d',' -f1 "$input_file" > "$output_file"
# Paste second column to the output file
cut -d',' -f2 "$input_file" | paste "$output_file" - > temp && mv temp "$output_file"
This script extracts the first and second columns from a CSV file and combines them side by side into a new file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line of the input file twice (once per cut command).
- How many times: Each line is processed twice, once for each column extraction.
As the number of lines in the input file grows, the script reads through all lines twice.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 line reads |
| 100 | About 200 line reads |
| 1000 | About 2000 line reads |
Pattern observation: The total work grows roughly twice as fast as the number of lines because each line is read two times.
Time Complexity: O(n)
This means the time to run the script grows linearly with the number of lines in the input file.
[X] Wrong: "Since there are two cut commands, the time complexity is squared, like O(n²)."
[OK] Correct: Each cut command reads the file once, so the total work is two times n, which is still linear, not squared.
Understanding how simple commands like cut and paste scale with input size helps you write efficient scripts and explain your reasoning clearly in real-world tasks.
What if we combined the two cut commands into one that extracts both columns at once? How would the time complexity change?
Practice
cut command do in bash scripting?Solution
Step 1: Understand the purpose of
Thecutcutcommand is used to extract sections from each line of input, usually by specifying fields or characters.Step 2: Compare with other commands
pastejoins lines side by side,grepdeletes or filters lines, andwccounts lines. So, onlycutextracts parts of lines.Final Answer:
Extracts specific parts of each line from a file or input -> Option BQuick Check:
cut= Extract parts [OK]
- Confusing cut with paste
- Thinking cut deletes lines
- Assuming cut counts lines
cut?Solution
Step 1: Identify correct option flags for cut
The-fflag specifies the field number, and-dspecifies the delimiter character.Step 2: Match syntax to extract 2nd field with ':' delimiter
cut -f 2 -d ':' filename uses-f 2and-d ':', which is the correct order and syntax.Final Answer:
cut -f 2 -d ':' filename -> Option AQuick Check:
Fields (-f) then delimiter (-d) [OK]
- Swapping -f and -d flags
- Using -c instead of -f for fields
- Putting delimiter as field number
file1.txt contains:apple
banana
cherryand
file2.txt contains:red
yellow
redWhat is the output of
paste file1.txt file2.txt?Solution
Step 1: Understand paste command behavior
pastejoins lines from two files side by side separated by a tab by default.Step 2: Combine lines from both files line by line
Line 1: apple + tab + red
Line 2: banana + tab + yellow
Line 3: cherry + tab + redFinal Answer:
apple red banana yellow cherry red -> Option CQuick Check:
paste joins lines side by side [OK]
- Thinking paste stacks lines vertically
- Assuming paste adds commas
- Ignoring tab separator default
cut -c 1-3 file.txtIt returns an error. Why?
Solution
Step 1: Check cut syntax for character ranges
cut -c 1-3is the correct syntax to extract characters 1 to 3.Step 2: Consider other reasons for error
If an error occurs, it is likely due to the file not existing or permission issues, not syntax.Final Answer:
The command is correct; error likely from missing file -> Option AQuick Check:
cut -c 1-3 is valid syntax [OK]
- Thinking cut -c doesn't accept ranges
- Using commas instead of dash for ranges
- Ignoring file existence or permissions
names.txt with:John
Jane
Doeand
ages.txt with:25
30You want to create a file
combined.txt where each line has name and age separated by a comma. Missing ages should show as 'N/A'. Which command sequence achieves this?Solution
Step 1: Use paste with comma delimiter
paste -d ',' names.txt ages.txtjoins lines with a comma.Step 2: Handle missing ages with sed
Lines missing age end with a comma only, sosed 's/,$/,N\/A/'replaces trailing comma with ',N/A'.Step 3: Redirect output to combined.txt
Using > combined.txt saves the result to the file.Final Answer:
paste -d ',' names.txt ages.txt | sed 's/,$/,N\/A/' > combined.txt -> Option DQuick Check:
paste + sed fixes missing fields [OK]
- Not handling missing age lines
- Using cut instead of paste for joining
- Forgetting to redirect output to file
Ready to prove it?
Pass the test and this pattern moves unknown
