LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 10: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, 10:20 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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, 10: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,477

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
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, 10: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, 10:35 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
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, 11:12 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
@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, 02:17 PM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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


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


All times are GMT -5. The time now is 10:53 AM.

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