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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
woolmilkporcCommented:
Here you go -
awk -F: ' /:!/ {print $1}' /etc/shadow
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.