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

  1. Extract Specific Fields:
    Displayed the second column of a file:

     cut -d ',' -f 2 file.csv
    
  2. Extract Specific Characters:
    Extracted characters from positions 3 to 7 in each line:

     cut -c 3-7 file.txt
    

Using the tr Command

  1. Translate Characters:
    Converted all lowercase letters to uppercase:

     echo "hello world" | tr 'a-z' 'A-Z'
    
  2. Delete Characters:
    Removed all digits from a string:

     echo "abc123def456" | tr -d '0-9'
    

Using the echo Command

  1. Print Text:
    Displayed a message on the terminal:

     echo "Welcome to Linux!"
    
  2. Print Variables:
    Displayed the value of an environment variable:

     echo $HOME
    

Using the export Command

  1. Set Environment Variables:
    Created a new variable:

     export MY_VAR="Hello, World!"
    
  2. Make Variables Available to Subshells:
    Verified the variable in a new shell session.

Using the source Command

  1. Execute a Script in the Current Shell:
    Loaded a script without creating a subshell:

     source script.sh
    

Using the history Command

  1. View Command History:
    Displayed the last 10 commands:

     history 10
    
  2. Reuse a Command:
    Re-executed a specific command using its history number:

     !15
    

Problems I Encountered

  1. Handling Delimiters with cut:
    The default tab delimiter caused incorrect outputs with CSV files.

  2. Understanding export and source:
    Differentiating their roles and behavior in shell environments was initially confusing.


How I Solved These Problems

  1. Custom Delimiters in cut:
    Used the -d option to specify a comma as the delimiter.

  2. Deep Dive into Shell Behavior:
    Explored detailed tutorials and practical examples to clarify the use of export and source.


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!