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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find a name

Status
Not open for further replies.

vti

Technical User
Feb 26, 2001
189
TR


Hi all
Ý have a file which includes users by groupnames like

============================================

Group1 :
User1
User2
User3

Group2 :
User4
User5
User6

Group3 :
...
..
.
============================================

I want to search a username by grep in a script from this file,and if username i am serching for is under "group1 :" script has to go exit but if it's under "Group2 :" script has to give me an echo output.But i don't know how to do ?

Thanks for any help.
 

Hello unixadmin,

if I quite understood your problem, you can do this with awk:

Code:
awk  '/Group1/  { group = 1; next }
/Group2/  { group = 2; next }
/Group[3-9]/  { group = 3; next }
group == 1  { next }
group == 2  { print }
group >= 3  { exit }'  inputfile

Works? If not - ask more.

Bye!

KP.
 

Oops,

My answer is wrong. I omitted this "...under "group1 :" script has to go exit."

I will try to make another solution.

Bye!

KP.
 

I think that I have a solution for you. If we search for "User4"...

Code:
awk '/Group1/  { group = 1 }
/Group2/  { group = 2 }
group == 1 && /User4/ { exit }
group == 2 && /User4/ { print }' inputfile

Insted "User4" you can use variable substitution.

I hope it works.

Bye!

KP.
 
The same with the user name passed with a variable ...

awk -v User='User4' '/Group1/ { group = 1 }
/Group2/ { group = 2 }
$1==User { if (group == 1) print ;
exit }' inputfile

Jean Pierre.
 
hi
It works well,thanks a lot
But i meant with script has to go exit ,Scipt has to disconnect telnet session if user4 not under Group1 .

So can i run an unix command by awk.?


Best Regards.
 
No, I don't think so.
However, if you just use kruneks and aigles code
in this manner:

perm=$(awk ' {
script code
}')

if [ "$perm" != group ]
then
exit 1 > /dev/null 2>&1
else
:
fi

It should work well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top