Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

piping search pattern to awk 1

Status
Not open for further replies.

gibson153

Technical User
Jul 20, 2001
29
US

I want to see who is online and then print information about each user from /etc/passwd/

awk -f $(who -qn 1) '{ print $0 }' /etc/passwd

any suggestion on how I can make this work?

David
 
This seems to work

who -qn 1 | awk '{print "/" $1 "/{print}"}' | awk -f - /etc/passwd

CaKiwi
 
Upon further review, it would be better to search for the user only at the beginning of the line in passwd to avoid finding lines which had the same string as a user name further along. Also the {print} is not necessary as it is the default. This results in the following solution:

who -qn 1 | awk '{print "/^" $1 "/"}' | awk -f - /etc/passwd

I believe some older awks do not allow the program to come from stdin. In this case, you would need to redirect the output from the first awk to a temporary file and use this file as the program for the second awk.

 
Here you go:

awk ' BEGIN {
"who" | getline info
gsub(/tty.*/, "" , info)

while ((getline pots < &quot;/etc/passwd&quot;) > 0) {

split(pots, names, &quot;:&quot;)
for (x in names)
if (info ~ names[x]) {
print pots
}

}
}'

Needs to be filtered more, maybe you can figure it out.
It does come up with the right info+.
gawk 3.1.0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top