Help with Regular Expression in grep

I'm trying to formulate a regular expression in grep to return expired account names from the shadow file.

If I have a line in /etc/shadow like the following:
netuser:!$blahblahblah
I want grep to return everything to the left of :

netuser, that is

Thanks
Matthew

mattinvtAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
woolmilkporcConnect With a Mentor Commented:
Here you go -
awk -F: ' /:!/ {print $1}' /etc/shadow
0
 
woolmilkporcCommented:
awk -F: '{print $1}' /etc/shadow
wmp
0
 
mattinvtAuthor Commented:
Almost there - I don't want to see lines that don't contain ":!"

So if I have
netuser1:!$blahblahblah
netuser2:$blahblahblah

the expression only returns netuser1
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
mattinvtAuthor Commented:
Awsome
0
 
tomaugerdotcomCommented:
My understanding of GREP is that it is used to print the entire line, not to extract parts of the line.

Why not try this Perl 1-liner?

perl -ne '/^([^:]+)/; print "$1\n"' /etc/shadow

perl -e says that the next argument is to be executed like a program
perl -n says to loop through each line of the file

so perl -ne says loop through each line of the file and execute the argument (inside '');

The perl part is two lines:

/^([^:]+)/;  # <-- this is a regular expression that starts at the beginning of the line and stops at the first ":"
print "$1\n"; # <--- this prints the result (captured inside the () in the previous line) and adds a newline

Hope this helps!
0
 
tomaugerdotcomCommented:
Seeing your previous response, my regexp needs to be modified to
/^([^:]+):!/;

But of course the awk solution is more elegant.
0
All Courses

From novice to tech pro — start learning today.