LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2016, 09:29 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Rep: Reputation: 1
Grep text in awk after pattern


I am trying to execute following command but I am not able to do it.

Code:
awk -F: '{ system("passwd -S " $1)}' /etc/passwd | grep LK
I want to list all Locked account but not using grep after awk.

Please help me to achieve this using same awk command.

I have tried
Code:
awk -F: '{ system("passwd -S " $1)/LK/}' /etc/passwd
awk -F: '{ system("passwd -S " $1)}/LK/' /etc/passwd
awk -F: '/LK/{ system("passwd -S " $1)}' /etc/passwd
awk -F: '{ system("passwd -S " $1)($1 == "LK")}{print $1}' /etc/passwd
Please help.
 
Old 03-11-2016, 10:12 AM   #2
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 3,075

Rep: Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066
Can I ask why are you using the separation character ':'?

On my system, the columns generated by passwd -S appear to be space-separated.

(I have other questions as well, but thought we'd start with that one )

Last edited by hydrurga; 03-11-2016 at 10:24 AM.
 
Old 03-11-2016, 10:22 AM   #3
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 499

Rep: Reputation: 188Reputation: 188
Hi there,

Try this:

Code:
awk -F: '{ "passwd -S " $1 | getline status; split(status,sf," "); if (sf[2] == "LK") print sf[1];}' /etc/passwd
The "getline" reads the output from the "passwd -S" command into the "status" variable, and split() separates the line into fields ("sf").

With recent versions of "passwd" you can show the status of all accounts in one go, making this possible:

Code:
passwd -S -a | awk '/ LK / {print $1}
 
Old 03-11-2016, 10:23 AM   #4
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 499

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by hydrurga View Post
Can I ask why are you using the separation character ':'?

On my system, the columns generated by passwd -S appear to be space-separated.
The separator is for parsing /etc/passwd, not "passwd -S" ;-)
 
Old 03-11-2016, 10:29 AM   #5
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 3,075

Rep: Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066
Quote:
Originally Posted by cliffordw View Post
The separator is for parsing /etc/passwd, not "passwd -S" ;-)
:-) Ok.

So, why doesn't

Code:
passwd -S -a | awk -F" " '$2 ~ /L/'
do the trick to achieve OP's intention to list accounts with locked passwords. Obviously I'm not understanding something here.

Edit: I've used L instead of LK because man passwd on my system indicates that only this letter indicates a locked password.

Last edited by hydrurga; 03-11-2016 at 10:32 AM. Reason: Explained L
 
Old 03-11-2016, 11:34 AM   #6
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 499

Rep: Reputation: 188Reputation: 188
Hi hydrurga,

I think it depends on your version of "passwd". On my openSUSE Tumbelweed laptop your command works ("-a" option is present, and output shows "L" only). On al older Centos 6 server I found that "-a" doesn't exist, and output shows "LK" as per unclesamcrazy's original post.
 
Old 03-11-2016, 11:38 AM   #7
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 3,075

Rep: Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066Reputation: 1066
Quote:
Originally Posted by cliffordw View Post
Hi hydrurga,

I think it depends on your version of "passwd". On my openSUSE Tumbelweed laptop your command works ("-a" option is present, and output shows "L" only). On al older Centos 6 server I found that "-a" doesn't exist, and output shows "LK" as per unclesamcrazy's original post.
Ah ok, hence the reason for having to access /etc/passwd. Thanks for explaining! (and so much for standardisation of the essentials )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] pattern for grep/sed/awk that matches lines that have several words but not another maddes.b Linux - Software 7 07-03-2013 11:08 AM
[SOLVED] how to extract a 2-line pattern from a file using awk, grep, etc. dcsmayei Linux - Newbie 9 06-09-2012 08:32 AM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
Need ideas on how to grep for pattern using awk rahulbgl Linux - Newbie 4 03-05-2007 03:07 PM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration