0
0
Bash Scriptingscripting~10 mins

cut and paste in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - cut and paste
Start
Use 'cut' to extract columns
Use 'paste' to join columns
Output combined result
End
The flow shows extracting parts of text with 'cut', then joining them side-by-side with 'paste' to create combined output.
Execution Sample
Bash Scripting
echo -e "a,b,c\nd,e,f" > file1.txt
cut -d',' -f1 file1.txt > col1.txt
cut -d',' -f3 file1.txt > col3.txt
paste col1.txt col3.txt
Extract first and third columns from a CSV file and paste them side by side.
Execution Table
StepCommandActionOutput
1echo -e "a,b,c\nd,e,f" > file1.txtCreate file1.txt with two lines
2cut -d',' -f1 file1.txt > col1.txtExtract first column (before first comma)col1.txt contains: a d
3cut -d',' -f3 file1.txt > col3.txtExtract third column (after second comma)col3.txt contains: c f
4paste col1.txt col3.txtCombine col1.txt and col3.txt side by sidea c d f
5EndNo more commandsExecution complete
💡 All commands executed; paste output shows combined columns.
Variable Tracker
Variable/FileStartAfter Step 1After Step 2After Step 3After Step 4Final
file1.txtdoes not exista,b,c d,e,fa,b,c d,e,fa,b,c d,e,fa,b,c d,e,fa,b,c d,e,f
col1.txtdoes not existdoes not exista da da da d
col3.txtdoes not existdoes not existdoes not existc fc fc f
paste outputnonenonenonenonea c d fa c d f
Key Moments - 3 Insights
Why do we use '-d',' and '-f' options with cut?
The '-d',' tells cut to use comma as the separator, and '-f' selects which column to extract. This is shown in steps 2 and 3 where cut extracts columns based on commas.
What does paste do with the two files?
Paste combines lines from two files side by side separated by a tab, as seen in step 4 where col1.txt and col3.txt lines are joined.
Why do we redirect cut output to files before pasting?
Paste works on files or streams line by line, so we save cut outputs to files first to combine them later, shown in steps 2 and 3 before step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does col1.txt contain after step 2?
A"c\nf"
B"a,b,c\nd,e,f"
C"a\nd"
D"a\nc"
💡 Hint
Check the 'Output' column for step 2 in the execution_table.
At which step does the paste command run?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the command 'paste col1.txt col3.txt' in the execution_table.
If we changed '-f3' to '-f2' in step 3, what would col3.txt contain?
A"c\nf"
B"b\ne"
C"a\nd"
D"d\nf"
💡 Hint
The '-f2' option extracts the second column, check original file1.txt content.
Concept Snapshot
cut -d',' -fN file.txt extracts column N using comma as delimiter
paste file1 file2 joins lines side by side with tabs
Use cut to get columns, save to files, then paste to combine
Output shows columns combined line by line
Useful for splitting and merging text data
Full Transcript
This example shows how to use the 'cut' command to extract specific columns from a text file using a delimiter, here a comma. We create a file with two lines of comma-separated values. Then we cut the first and third columns into separate files. Finally, we use 'paste' to join these two files side by side, producing combined output. The execution table traces each step, showing file contents and command outputs. Key moments clarify why options like '-d' and '-f' are used with cut, and how paste combines files line by line. The visual quiz tests understanding of file contents after each step and the effect of changing cut options. The snapshot summarizes the commands and their purpose for quick reference.