LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-07-2016, 10:22 AM   #1
who10
LQ Newbie
 
Registered: Sep 2016
Location: Midwest USA
Posts: 9

Rep: Reputation: Disabled
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!
 
Old 10-08-2016, 04:01 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
You could try awk / perl / python or any number of commands / languages to manipulate the data into the desired format.
 
Old 10-10-2016, 10:06 AM   #3
who10
LQ Newbie
 
Registered: Sep 2016
Location: Midwest USA
Posts: 9

Original Poster
Rep: Reputation: Disabled
I'll try that. Thanks.
 
Old 10-10-2016, 10:13 PM   #4
Bhagyesh
Member
 
Registered: Jul 2009
Posts: 35

Rep: Reputation: 17
Lightbulb 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 10:18 PM.
 
Old 10-11-2016, 10:17 AM   #5
who10
LQ Newbie
 
Registered: Sep 2016
Location: Midwest USA
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Bhagyesh View Post
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.
 
Old 10-11-2016, 10:23 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,602

Rep: Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361Reputation: 4361
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>
 
Old 10-11-2016, 10:54 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,364

Rep: Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980
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.
 
1 members found this post helpful.
Old 10-11-2016, 11:11 AM   #8
who10
LQ Newbie
 
Registered: Sep 2016
Location: Midwest USA
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-11-2016, 03:04 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Here's a reference to help :- http://www.gnu.org/software/gawk/man...ode/index.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
ldapsearch not working vjy Linux - General 2 05-04-2015 11:30 AM
ldapsearch vs Apache DS LDAP browser (ldapsearch not working properly) eyemole80 Linux - Server 0 02-18-2014 03:45 PM
Sort attributes by name for each entry in ldapsearch output (ldap database export) llattan Linux - Enterprise 0 04-04-2009 09:50 PM
Best scripting language for working with openldap-tools (ldapsearch, etc) kenneho Linux - Server 2 11-21-2008 02:31 AM
ls -la command output is not working properly ratul_11 General 1 02-27-2008 01:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration