LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-11-2011, 09:02 AM   #1
alancampbell6
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Rep: Reputation: Disabled
Manipulate output


Code:
"***Search command***" | grep manager | awk '{print $2, $3}'
output:
CN=Bloggs\, Joe,OU=Users,OU=LTMK-E,DC=ad,DC=nexty,DC=com

required output:
Joe Bloggs

Any ideas?
 
Old 10-11-2011, 09:20 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You can use nested awk statments:

echo CN=Bloggs\, Joe,OU=Users,OU=LTMK-E,DC=ad,DC=nexty,DC=com |awk -F\\ '{print $1,$2}' |awk -F= '{print $2,$3}' |awk -F, '{print $2,$1}' |awk '{print $1" "$2}'

The first awk separates based on the backslash, the second one separates based on the equal sign, the third one separates based on comma and the final one separates on whitespace to remove the space in front of Joe.
 
1 members found this post helpful.
Old 10-11-2011, 09:26 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
It looks like your data file is a CSV (comma-separated-variables)?

What you need to do is get rid of a few things and change the commas to spaces. This is easily done with sed:
Code:
grep manager | sed 's/CN=//;s/\\//;s/,/ /g' | awk '{ print $2 " " $1 }'
ought to do it.

You could fiddle around with AWK, but sed is quicker and easier.

Hope this helps some.
 
1 members found this post helpful.
Old 10-11-2011, 09:27 AM   #4
alancampbell6
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
You can use nested awk statments:

echo CN=Bloggs\, Joe,OU=Users,OU=LTMK-E,DC=ad,DC=nexty,DC=com |awk -F\\ '{print $1,$2}' |awk -F= '{print $2,$3}' |awk -F, '{print $2,$1}' |awk '{print $1" "$2}'

The first awk separates based on the backslash, the second one separates based on the equal sign, the third one separates based on comma and the final one separates on whitespace to remove the space in front of Joe.
Thanks MensaWater. Perfect!!!
 
Old 10-11-2011, 09:35 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Are all entries in the same format, where the 'CN' field has the 'Surname, Firstname' arrangement? If yes, then this might do the trick:
Code:
awk 'BEGIN{FS="[\\\=, ]"}{print $5 " " $2}'
However, this relies on all records having the same consistent format. Without a much larger sample size, no one here can know how to make a fail-proof version.

--- rod.
 
Old 10-11-2011, 10:12 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@theNbomr - that didn't seem to work for me Needed an extra back slash and use a different field:
Code:
awk 'BEGIN{FS="[\\\\,= ]+"}/manager/{print $3,$2}'
Threw in 'manager' as grep is a wasted item here.
 
Old 10-11-2011, 01:17 PM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by alancampbell6 View Post
Thanks MensaWater. Perfect!!!
No problem - the sed solution might be a bit more elegant with less impact on your system. I just did the awk as a quick and dirty.
 
  


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
[SOLVED] how to manipulate sysctl_tcp_... variables hayf Programming 1 08-23-2011 03:48 AM
[SOLVED] can i manipulate a .pdf in linux? oznola Linux - Software 2 07-26-2010 12:00 AM
manipulate the floppy bruse Mandriva 1 02-22-2005 07:49 AM
Please translate pstree output and inform how to manipulate it Frybyte Linux - General 1 06-19-2004 09:26 PM
need to manipulate dates clsonnt Programming 5 08-19-2003 10:07 AM

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

All times are GMT -5. The time now is 12:08 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