cancel
Showing results for 
Search instead for 
Did you mean: 
TudorRaduta
Community Manager
Community Manager
  • 401 Views

That moment regular expressions finally click...

Monday Mission: Mastering `grep` and Regex

Happy Monday, everyone!

Let's kick off the week with a new RHCSA study mission. This week, we're focusing on a task that comes straight from the "Understand and use essential tools" section of the exam objectives: using grep with regular expressions (regex) to analyze text.

The Mission

My goal this week isn't to memorize every complex regex pattern. Instead, my mission is to master the fundamental building blocks, like the special meaning of ^ (start of line), $ (end of line), . (any single character), and [ ] (a set of characters).

The Real Map: man 7 regex

(The definitive guide to regex syntax is built right into the system!)

Your Turn!

  1. What RHCSA topic is your main focus this week?
  2. Quick challenge: What grep command would you use on the /etc/ssh/sshd_config file to find all lines that are not commented out and not blank?

Let's get this week started!

8 Replies
ricardodacosta
Moderator
Moderator
  • 347 Views

The wrong answer:

grep -v '#' /etc/ssh/sshd_config

The easy answer: 

grep '^[^#]' /etc/ssh/sshd_config

The smart answer: Add this alias to your shell login script:

decomment='grep -Ev "^[[:space:]]*((#|;|//).*)?$" '
TudorRaduta
Community Manager
Community Manager
  • 161 Views

@ricardodacosta that’s a great breakdown and you can really see the journey from “it works” to “it works right.” I really like that decomment alias too; it’s the kind of smart shortcut you only discover after dealing with enough messy configs.

Chetan_Tiwary_
Community Manager
Community Manager
  • 110 Views

For beginners, what @ricardodacosta answered :

1. 

grep -v '#' /etc/ssh/sshd_config

 

This command tells to show lines that DO NOT contain the character # anywhere, so if a valid config item like this is present in sshd_config :


"PasswordAuthentication Yes #enables password authentication"


This command will ignore this as well - which we dont want as per the question.

 

2. 

grep '^[^#]' /etc/ssh/sshd_config

This is an easy answer because it greps lines that DO NOT start with # ( which is what is meant by '^[^#]'   --> Match any line that begins with a character that is NOT a hash symbol (#)

But it still ignores those valid config items who are , lets suppose, have whitespace at the beginning. 

3.

decomment='grep -Ev "^[[:space:]]*((#|;|//).*)?$" '

 

This specific search pattern is designed to find and match a few kinds of lines:

Completely empty lines or lines that contain only whitespace (like spaces or tabs).

Any line that starts with common comment markers like a hash symbol (#), a semicolon (;), or double slashes (//), even if there's some whitespace before the marker.

Because the command uses grep -Ev, it performs an inverse match. This means it excludes any line that matches those patterns.

In short, when you use this command, the result is that you only see the lines that contain active, meaningful text, filtering out all blank space and comments.

Also, option -E makes it an Extended Regular Expression (so you can use |, (), ? without backslashes ).

 

ricardodacosta
Moderator
Moderator
  • 108 Views

Thanks @Chetan_Tiwary_ 

I must add, item 3 in my solutions list is highly scalable. 

If you come across additional comment characters, you can pipe separate them.

decomment='grep -Ev "^[[:space:]]*((#|;|//|\`\`\`).*)?$" '
Chetan_Tiwary_
Community Manager
Community Manager
  • 31 Views

oh yes, beauty!! @ricardodacosta !

0 Kudos
Blue_bird
Starfighter Starfighter
Starfighter
  • 207 Views

Few years back..when I was new to linux : "regular expressions -- bit scary..!". Not only in Linux, regular expressions are used even with Netwoking protocols like BGP as well and almost have the similar purpose. Later on...It took me some time and efforts to learn and understand them.

grep -Ev '^\s*(#|$)' /etc/ssh/sshd_config

Thanks

TudorRaduta
Community Manager
Community Manager
  • 161 Views

@Blue_bird that’s such a relatable story. I think everyone’s had that “regex is scary” phase at first. Once it clicks, you start seeing patterns everywhere, even outside Linux.

And that

 grep -Ev '^\s*(#|$)' 

command is spot on.

Chetan_Tiwary_
Community Manager
Community Manager
  • 20 Views

@Blue_bird Nice attempt, your command  shows non-blank, non-commented lines in the sshd_config. But what happens when the file contains something like this :

PermitRootLogin yes  # allow root

will it be able to show this line as well ?

0 Kudos
Join the discussion
You must log in to join this conversation.