LinuxQuestions.org
Visit Jeremy's Blog.
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 09-28-2011, 04:18 PM   #1
suchi123
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
need help to write script


dn: cn=abc,dc=ex,dc=com
cn: abc
cn: abc xyz
dn: cn=klm,dc=ex,dc=com
cn: lkm hello
cn: klm
This is what i have, i need to get the cn which matches the cn in dn: line. and delete the other cn line.

Output should be:
dn: cn=abc,dc=ex,dc=com
cn: abc
dn: cn=klm,dc=ex,dc=com
cn: klm

please help me with this, i would really appreciate your help. Thanks in advance.
 
Old 09-28-2011, 04:48 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
A gawk solution:
Code:
awk '/dn:/{value=gensub(/.*cn=([^,]+),.*/,"\\1",1);print}/cn:/&&$NF==value' file
 
Old 09-28-2011, 04:52 PM   #3
suchi123
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for your reply, but i am getting this syntax error...

bash-3.00$ awk '/dn:/{value=gensub(/.*cn=([^,]+),.*/,"\\1",1);print}/cn:/&&$NF==value' modify.txt
awk: syntax error near line 1
awk: illegal statement near line 1

Can you please fix this for me...
 
Old 09-29-2011, 10:17 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,932

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by suchi123 View Post
Thanks for your reply, but i am getting this syntax error...

bash-3.00$ awk '/dn:/{value=gensub(/.*cn=([^,]+),.*/,"\\1",1);print}/cn:/&&$NF==value' modify.txt
awk: syntax error near line 1
awk: illegal statement near line 1

Can you please fix this for me...
The solution that colucix posted works fine on openSUSE 11.4 using GNU Awk 3.1.6, and your sample input data. What version/distro of Linux are you running, and what version of awk?

Also, you may want to try writing your own scripts, and reading one of the THOUSANDS of scripting tutorials that you can easily find. We don't mind HELPING you, but you've not shown any effort into doing this yourself, but expect us to do it for you.
 
  


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
How to write a script? matthes138 Linux - Newbie 3 07-04-2009 12:04 PM
Please help me write script sharp859 Linux - Newbie 5 04-16-2009 06:41 PM
Better way to write this script faizlo Programming 6 01-08-2009 03:25 AM
how write a script saif Linux - Newbie 8 07-27-2007 01:31 PM


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