Day 19 Learnings: Text Processing and Manipulation in Linux
Today's Linux session focused on essential commands for text processing and shell interaction. We covered tools to manipulate text, interact with the shell, and retrieve command history effectively.
What I Learned This Week
cut
Command: Extracts specific sections of lines from files or input streams.tr
Command: Translates or deletes characters.echo
Command: Prints text or variables to the terminal.export
Command: Sets environment variables.source
Command: Executes commands from a file in the current shell.history
Command: Displays a list of previously executed commands.
Steps I Followed
Using the cut
Command
Extract Specific Fields:
Displayed the second column of a file:cut -d ',' -f 2 file.csv
Extract Specific Characters:
Extracted characters from positions 3 to 7 in each line:cut -c 3-7 file.txt
Using the tr
Command
Translate Characters:
Converted all lowercase letters to uppercase:echo "hello world" | tr 'a-z' 'A-Z'
Delete Characters:
Removed all digits from a string:echo "abc123def456" | tr -d '0-9'
Using the echo
Command
Print Text:
Displayed a message on the terminal:echo "Welcome to Linux!"
Print Variables:
Displayed the value of an environment variable:echo $HOME
Using the export
Command
Set Environment Variables:
Created a new variable:export MY_VAR="Hello, World!"
Make Variables Available to Subshells:
Verified the variable in a new shell session.
Using the source
Command
Execute a Script in the Current Shell:
Loaded a script without creating a subshell:source script.sh
Using the history
Command
View Command History:
Displayed the last 10 commands:history 10
Reuse a Command:
Re-executed a specific command using its history number:!15
Problems I Encountered
Handling Delimiters with
cut
:
The default tab delimiter caused incorrect outputs with CSV files.Understanding
export
andsource
:
Differentiating their roles and behavior in shell environments was initially confusing.
How I Solved These Problems
Custom Delimiters in
cut
:
Used the-d
option to specify a comma as the delimiter.Deep Dive into Shell Behavior:
Explored detailed tutorials and practical examples to clarify the use ofexport
andsource
.
Resources I Used
Linuxize: Cut Command Tutorial
GeeksforGeeks: tr Command Examples
TutorialsPoint: Linux Command Basics
Conclusion
These commands streamline text processing and shell scripting in Linux. Tools like cut
and tr
are indispensable for data manipulation, while export
and source
are vital for managing shell environments. The history
command is a productivity booster for repetitive tasks.
Looking forward to exploring more Linux commands and their practical applications in upcoming sessions!