LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   again stucked with text processing (sed/awk/perl), copy the line and change (https://www.linuxquestions.org/questions/programming-9/again-stucked-with-text-processing-sed-awk-perl-copy-the-line-and-change-698265/)

rahmathullakm 01-19-2009 01:57 AM

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

chakka.lokesh 01-19-2009 03:35 AM

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

rahmathullakm 01-19-2009 09:47 AM

Thank you lokesh, it worked for me... now can u pls explain that command. I didnt understand how it works...???

Sergei Steshenko 01-19-2009 12:59 PM

Quote:

Originally Posted by rahmathullakm (Post 3413839)
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' ?

colucix 01-19-2009 01:53 PM

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.


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