LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-19-2009, 02:57 AM   #1
rahmathullakm
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Rep: Reputation: 0
again stucked with text processing (sed/awk/perl), copy the line and change


from the following file;

dn: CN=Muhammad Hadhi K.M,OU=IT Dept,OU=Example Company H.O,DC=example,DC=com
cn: Muhammad Hadhi K.M
sn: Hadhi
l: Calicut
title: IT Manager
postalCode: 5456
postOfficeBox: 3434
physicalDeliveryOfficeName: IT Department
telephoneNumber: +111 1 111111 Ext:111
facsimileTelephoneNumber: +222 2 222222
givenName: Muhammad
department: IT
company: Example Company Ltd.
proxyAddresses: SMTP:hadhi@example.com
streetAddress: P.O.Box 3434
mobile: +4444 4444 4444


i need to copy the value of cn: and put a new line under the cn: line with the "displayName: Muhammad Hadhi K.M"

cn: Muhammad Hadhi K.M
displayName: Muhammad Hadhi K.M


there are almost 1500 entries. the cn: value and displayName: value must be same for each addresses


any idea??? i think perl can do this task easily
 
Old 01-19-2009, 04:35 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
sed -r 's/cn\:(.*)/&\ndisplayname:\1/' input_file_name

Last edited by chakka.lokesh; 01-19-2009 at 04:40 AM.
 
Old 01-19-2009, 10:47 AM   #3
rahmathullakm
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Thank you lokesh, it worked for me... now can u pls explain that command. I didnt understand how it works...???
 
Old 01-19-2009, 01:59 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by rahmathullakm View Post
Thank you lokesh, it worked for me... now can u pls explain that command. I didnt understand how it works...???
Have you tried 'man sed' ?
 
Old 01-19-2009, 02:53 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To learn sed I suggest Sed: An Introduction And Tutorial. Here is an alternative solution in awk:
Code:
awk '{print} /cn:/{name = gensub("cn:","displayName:",1,$0); print name}' file
to actually modify the file you have to redirect the output to another file and then substitute the original one. The advantage of sed is that you can edit the file in place using the -i option.
 
  


Reply

Tags
awk, perl, sed


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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
Text substitution and processing with sed and awk shanecraddock@gmail.com Linux - Newbie 1 12-18-2008 12:34 PM
Modify a text files with awk/sed/perl climber75 Programming 15 08-05-2008 04:35 PM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 12:53 PM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM


All times are GMT -5. The time now is 07:37 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration