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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-07-2016, 09:22 AM
|
#1
|
LQ Newbie
Registered: Sep 2016
Location: Midwest USA
Posts: 9
Rep: 
|
Working with output from ldapsearch command
Using ldapsearch, I've built the following command to get what I need.
ldapsearch -x -LLL -E pr=200/noprompt -h abc-loc.somecompany.com -D "account@somecompany.com" -w password -b "ou=End Users,ou=Accounts,dc=abc,dc=somecompany,dc=com" -s sub '(distinguishedName=CN=Bob\\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com)' displayName saMAccountName telephoneNumber mail department departmentNumber l st
Results are:
dn: CN=Bob\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com
l: sometown
st: somestate
telephoneNumber: (123)456-7890
displayName: Bob, Billy J
department: Handform
sAMAccountName: b123456
mail: billy.j.bob@somecompany.com
departmentNumber: 5432
Now, I want to do something with this output, for example, write to a .txt file in a format such as:
Bob, Billy J|b123456|(123)456-7890|billy.j.bob@somecompany.com|Handform|5432|Somestate|Sometown
I know I can redirect the output of the ldapsearch to a file, but I'm not sure what the best route would be to format the output into a line similar to what I've shown above. Any pointers/opinions would help at this point. Thanks!
|
|
|
10-08-2016, 03:01 AM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,036
|
You could try awk / perl / python or any number of commands / languages to manipulate the data into the desired format.
|
|
|
10-10-2016, 09:06 AM
|
#3
|
LQ Newbie
Registered: Sep 2016
Location: Midwest USA
Posts: 9
Original Poster
Rep: 
|
I'll try that. Thanks.
|
|
|
10-10-2016, 09:13 PM
|
#4
|
Member
Registered: Jul 2009
Posts: 35
Rep:
|
Give this a try...
Code:
$$ cat /tmp/temp.txt
dn: CN=Bob\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com
l: sometown
st: somestate
telephoneNumber: (123)456-7890
displayName: Bob, Billy J
department: Handform
sAMAccountName: b123456
mail: billy.j.bob@somecompany.com
departmentNumber: 5432
$$ cat /tmp/tt.txt | sed -r 's/\\,|,|CN=|OU*//g' |awk -F "|=|:" '{printf $2"|"}'
Bob Billy J| sometown| somestate| (123)456-7890| Bob Billy J| Handform| b123456| billy.j.bob@somecompany.com| 5432|
Last edited by Bhagyesh; 10-10-2016 at 09:18 PM.
|
|
|
10-11-2016, 09:17 AM
|
#5
|
LQ Newbie
Registered: Sep 2016
Location: Midwest USA
Posts: 9
Original Poster
Rep: 
|
Quote:
Originally Posted by Bhagyesh
Code:
$$ cat /tmp/temp.txt
dn: CN=Bob\, Billy J,OU=End Users,OU=Accounts,DC=abc,DC=somecompany,DC=com
l: sometown
st: somestate
telephoneNumber: (123)456-7890
displayName: Bob, Billy J
department: Handform
sAMAccountName: b123456
mail: billy.j.bob@somecompany.com
departmentNumber: 5432
$$ cat /tmp/tt.txt | sed -r 's/\\,|,|CN=|OU*//g' |awk -F "|=|:" '{printf $2"|"}'
Bob Billy J| sometown| somestate| (123)456-7890| Bob Billy J| Handform| b123456| billy.j.bob@somecompany.com| 5432|
|
I'll need to research sed and awk to break this down and understand what each section is doing.
|
|
|
10-11-2016, 09:23 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,011
|
I would suggest you to use one single tool, like awk/python/perl/whatever, do not mix them! Probably you can do something like this:
Code:
awk (pseudo code)
/look for string/ { var1=$2 }
/look for another string/ { var2=$2 }
/look for final string/ { print formatted var1 var2 .... $2 } <filename>
|
|
|
10-11-2016, 09:54 AM
|
#7
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,553
|
Yep, I am with pan64 on this, as it allows for changing the order of the fields.
Code:
BEGIN {FS=": "; OFS="|"}
$1 == "l" {locality = $2}
$1 == "st" {state = $2}
$1 == "telephoneNumber" {telephoneNumber = $2}
$1 == "displayName" {displayName = $2}
$1 == "department" {department = $2}
$1 == "sAMAccountName" {sAMAccountName = $2}
$1 == "mail" {mail = $2}
$1 == "departmentNumber" {
print displayName,sAMAccountName,telephoneNumber,mail,department,$2,state,locality}
Put it in a file and use awk with the -f option.
|
|
2 members found this post helpful.
|
10-11-2016, 10:11 AM
|
#8
|
LQ Newbie
Registered: Sep 2016
Location: Midwest USA
Posts: 9
Original Poster
Rep: 
|
Being a newbie, I think it would be easier for me to focus on one tool as you've stated above. I'll look at awk based on what you've shown and go from there.
|
|
|
10-11-2016, 02:04 PM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,036
|
|
|
|
All times are GMT -5. The time now is 11:19 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|