LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep text in awk after pattern (https://www.linuxquestions.org/questions/linux-newbie-8/grep-text-in-awk-after-pattern-4175574610/)

unclesamcrazy 03-11-2016 09:29 AM

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.

hydrurga 03-11-2016 10:12 AM

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 ;))

cliffordw 03-11-2016 10:22 AM

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}

cliffordw 03-11-2016 10:23 AM

Quote:

Originally Posted by hydrurga (Post 5513867)
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" ;-)

hydrurga 03-11-2016 10:29 AM

Quote:

Originally Posted by cliffordw (Post 5513873)
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.

cliffordw 03-11-2016 11:34 AM

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.

hydrurga 03-11-2016 11:38 AM

Quote:

Originally Posted by cliffordw (Post 5513900)
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 ;))


All times are GMT -5. The time now is 05:35 PM.