Mastering Linux Commands: sudoers, Shell Scripting, and Text Processing with awk, grep, and sed

Linux offers powerful tools for system administration, shell scripting, and text processing, making it a versatile operating system for both developers and system administrators. This blog will guide you through advanced Linux commands and scripting techniques, including managing the sudoers file, utilizing text processing tools like awk, grep, and sed, and handling common Linux errors.


1. Managing User Privileges with sudoers File

What is the sudoers file?

The sudoers file is a special file that controls the permissions of users to run commands with superuser (root) privileges. By editing this file, you can grant or restrict sudo access to specific users.

How to Edit the sudoers File

To safely edit the sudoers file, use the visudo command:

sudo visudo

This command opens the sudoers file in a safe mode that checks for syntax errors before saving.

Granting a User Full sudo Privileges

To allow a user to run any command with sudo without a password prompt, add the following line:

username ALL=(ALL:ALL) ALL

Replace username with the actual username. Save the changes using:

  • Ctrl + O to save

  • Press Enter to confirm

  • Ctrl + X to exit

Allowing Specific Commands Without Password

If you want a user to execute specific commands (like reboot) without a password:

username ALL=(ALL) NOPASSWD: /sbin/reboot

2. Basics of Shell Scripting

Shell scripting is a powerful way to automate tasks in Linux. Let's explore some essential text processing commands used in shell scripts.


3. Text Processing with awk

awk is a text processing language used for pattern scanning and data extraction. It's widely used to filter and manipulate text data.

Syntax of awk

awk 'pattern {action}' filename

Examples of awk Usage:

Given a file 1.txt with the following content:

John 35 Marketing 5000
Alice 29 Sales 5000
Bob 45 Engineering 6000
Carol 31 HR 4800
David 28 Sales 5200
  • Print Names with Salary Greater than 5000

      awk '$4 > 5000 {print $1, $4}' 1.txt
      # Output: Bob 6000 David 5200
    
  • Filter Records with "Sales" Department

      awk '$3 == "Sales" {print $0}' 1.txt
      # Output: Alice 29 Sales 5000
    
  • Calculate Total Salary

      awk '{sum += $4} END {print "Total Salary:", sum}' 1.txt
      # Output: Total Salary: 26000
    
  • Increase Salary by 10%

      awk '{ $4 = $4 * 1.1; print $1, $4}' 1.txt
      # Output: John 5500 Alice 5500 Bob 6600 Carol 5280 David 5720
    

4. Searching Text with grep

grep is used for searching specific patterns within files.

Syntax of grep

grep [option] pattern filename

Examples of grep Usage:

Given a file 2.txt with the following content:

Alice 29 Sales New York
Bob 35 Engineering Chicago
Carol 28 Marketing San Francisco
David 45 Sales New York
Eve 33 Engineering New York
  • Find Lines Containing "Sales"

      grep 'Sales' 2.txt
    
  • Case-Insensitive Search for "sales"

      grep -i 'sales' 2.txt
    
  • Print Line Numbers Containing "Engineering"

      grep -n 'Engineering' 2.txt
    
  • Count Occurrences of "New York"

      grep -c 'New York' 2.txt
    
  • Search for Multiple Patterns (Sales or Marketing)

      grep -E 'Sales|Marketing' 2.txt
    

5. Stream Editing with sed

sed is a stream editor for filtering and transforming text.

Syntax of sed

sed 'command' filename

Examples of sed Usage:

Given a file 4.txt:

Alice 29 Sales New York
Bob 35 Engineering Chicago
Carol 28 Marketing San Francisco
David 45 Sales New York
  • Replace "Sales" with "Support"

      sed 's/Sales/Support/' 4.txt
    
  • Delete Lines Containing "Marketing"

      sed '/Marketing/d' 4.txt
    
  • Insert Header Line

      sed '1i\Name Age Dept Location' 4.txt
    
  • Append Text After Line 3

      sed '3a\This is a new line' 4.txt
    

6. Error Handling in Linux

Error handling is essential for troubleshooting system issues. Here are some common errors and how to resolve them:

  • Package Not Found

    If a package is not found, try updating your package lists:

      sudo apt update
    
  • Unable to Locate File

    Ensure the file path is correct or use find:

      find / -name filename
    
  • Website is Not Available

    Use ping to check network connectivity:

      ping google.com
    
  • SSH Connection Issues

    Check if the SSH port (22) is open using telnet:

      telnet public_ip 22
    

    If the connection fails, ensure SSH is enabled and the firewall is configured properly.


Conclusion

Understanding these Linux commands and shell scripting techniques can significantly boost your productivity and efficiency as a developer or system administrator. Whether you're managing user privileges with sudoers, automating tasks with shell scripts, or processing text with awk, grep, and sed, mastering these tools will give you an edge in handling complex tasks.

Feel free to share your thoughts or questions in the comments below! ๐Ÿš€

0
Subscribe to my newsletter

Read articles from Krishnat Ramchandra Hogale directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Krishnat Ramchandra Hogale
Krishnat Ramchandra Hogale

Hi! Iโ€™m Krishnat, a Senior IT Associate specializing in Performance Engineering at NTT DATA SERVICES. With experience in cloud technologies, DevOps, and automation testing, I focus on optimizing CI/CD pipelines and enhancing infrastructure management. Currently, I'm expanding my expertise in DevOps and AWS Solutions Architecture, aiming to implement robust, scalable solutions that streamline deployment and operational workflows.