LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2009, 03:28 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
perl or awk question


Hi everyone,

Could someone help me to print this info? I tried some awk but it was not printing well.

I used this awk statement with different trials but it was not printing it right.

awk '
/^dn:/ { dn = $0 }
/ou=test1|usergroup=cust1/ { print dn;}' input > output




input file:

dn: uid=user1, ou=test1,o=test.com
usergroup: cust1

dn: uid=user2, ou=test1,o=test.com
usergroup: cust1

dn: uid=user3, ou=test1,o=test.com
usergroup: cust1

dn: uid=user4, ou=test1,o=test.com
usergroup: abc1

dn: uid=user4, ou=test1,o=test.com
usergroup: abc1

.
.
.

desired: to print users having "ou=test1 and usergroup: cust1"

output:

user1
user2




Thanks
 
Old 09-14-2009, 04:08 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
perl -ne '($user,$ou) = /.*uid=(.*?), ou=(.*?),.*/ if /^dn/; print "$user\n" if /^usergroup: cust1/ && $ou eq "test1";' /tmp/input.txt
 
Old 09-14-2009, 04:30 PM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by estabroo View Post
perl -ne '($user,$ou) = /.*uid=(.*?), ou=(.*?),.*/ if /^dn/; print "$user\n" if /^usergroup: cust1/ && $ou eq "test1";' /tmp/input.txt
I appreciate for the great liner code but one more thing can you check please? How to define ou in the line? It can be different in a large file over 15GB.

Thanks again
 
Old 09-14-2009, 04:38 PM   #4
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
I appreciate for the great liner code but one more thing can you check please? How to define ou in the line? It can be different in a large file over 15GB.

Thanks again
pleae ignore it...it is working...thanks a lot...
 
Old 09-16-2009, 02:47 PM   #5
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
I appreciate for the great liner code but one more thing can you check please? How to define ou in the line? It can be different in a large file over 15GB.

Thanks again

If I wanted to negate an attribute in perl, how can I do it?

For example, if I want to filter out "ou=est1" and "ou=test2" and print any other uid's having usergroup: cust1

Please let me know your opinion...Thanks again...


perl -ne '($user,$ou) = /.*uid=(.*?), ou=(.*?),.*/ if /^dn/; print "$user\n" if /^usergroup: cust1/ && $ou eq "test1";' /tmp/input.txt
 
Old 09-16-2009, 04:20 PM   #6
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
use ne instead of eq or put a ! in front of the expression !(...)

... && ($ou ne "test1") && ($ou ne "test2") ...
 
Old 09-16-2009, 09:36 PM   #7
huchunqiang
LQ Newbie
 
Registered: Aug 2009
Location: Beijing,China
Posts: 3

Rep: Reputation: 0
awk -F " |=" 'BEGIN{RS="dn"}$5~/test1/&&$NF~/cust1/{print $3}' urfile | sed 's/,$//'
 
  


Reply



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
Shell Script / awk / perl Question cmontr Programming 11 06-25-2008 01:00 PM
awk / perl script help cmontr Programming 3 05-22-2008 04:15 PM
maximum value using perl or awk!! visitnag Linux - Newbie 3 04-11-2008 12:51 PM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 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
Open Source Consulting | Domain Registration