LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-02-2008, 09:26 AM   #16
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Original Poster
Rep: Reputation: 15

Quote:
Originally Posted by radoulov View Post
You should read more carefully my posts Anyway, as you insist to use the code on one line,
try this:
Code:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10 }' RS= OFS=: file
Hi Radoulov,

Can you help me to add a field (profession: technician) in this file by awk command?

dn: cn=10,ou=work,o=dom.com
cn: denis.vezina@dom.com
sn: vezina
givenName: denis
mail: denis.vezina@dom.com
displayName: denis vezina
profession: technician

dn: cn=20,ou=work,o=dom.com
cn: jean.paul@dom.com
sn: Paul
givenName: jean
mail: jean.paul@dom.com
displayName: jean paul
profession: technician

Thanks a lot,

Haydar
 
Old 08-02-2008, 09:52 AM   #17
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Code:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10,$14}' RS= OFS=: file
 
Old 08-02-2008, 10:39 PM   #18
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov View Post
Code:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10,$14}' RS= OFS=: file
Hi Radoulov,

Hi Radoulov,

Sorry for my last request, I think that I did not explain it more.

I have this file:

#cat file1
dn: cn=10,ou=work,o=dom.com
cn: denis.vezina@dom.com
sn: vezina
givenName: denis
mail: denis.vezina@dom.com
displayName: denis vezina

dn: cn=20,ou=work,o=dom.com
cn: jean.paul@dom.com
sn: Paul
givenName: jean
mail: jean.paul@dom.com
displayName: jean paul


dn: cn=30,ou=work,o=dom.com
cn: john.marc@dom.com
sn: Marc
givenName: john
mail: john.marc@dom.com
displayName: john marc

and I have another file;

#cat file2
physician
technician
doctor

I want to add or concatenate in file1 another field named Profession from file2.

My file file1 will contain these data after running the command:

dn: cn=10,ou=work,o=dom.com
cn: denis.vezina@dom.com
sn: vezina
givenName: denis
mail: denis.vezina@dom.com
displayName: denis vezina
profession: physician

dn: cn=20,ou=work,o=dom.com
cn: jean.paul@dom.com
sn: Paul
givenName: jean
mail: jean.paul@dom.com
displayName: jean paul
profession: technician

dn: cn=30,ou=work,o=dom.com
cn: john.marc@dom.com
sn: Marc
givenName: john
mail: john.marc@dom.com
displayName: john marc
profession: doctor

Can you help me to add or concatenate this field (profession) from file2 to file1 by awk command?

Thanks a lot,

Haydar
 
Old 08-02-2008, 10:46 PM   #19
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Haydar,

How do YOU think it should be done? So far, it looks like everyone else is doing your work, and you are doing none of it.

Don't take advantage of peoples goodwill. Spend some time reading the GNU awk manual, and post some of YOUR suggestions about how you think this problem can be solved.
 
Old 08-03-2008, 01:08 AM   #20
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by radoulov View Post
Code:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10,$14}' RS= OFS=: file
Hi Radoulov,

I found a solution for my previous request to you. I performed it in 2 steps or 2 commands:

I have to concatenate two files:

#cat file1
dn: cn=10,ou=work,o=dom.com
cn: denis.vezina@dom.com
sn: vezina
givenName: denis
mail: denis.vezina@dom.com
displayName: denis vezina

dn: cn=20,ou=work,o=dom.com
cn: jean.paul@dom.com
sn: Paul
givenName: jean
mail: jean.paul@dom.com
displayName: jean paul


dn: cn=30,ou=work,o=dom.com
cn: john.marc@dom.com
sn: Marc
givenName: john
mail: john.marc@dom.com
displayName: john marc


#cat file2
physician
technician
doctor

First step, I run this command:

awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10,$12}' RS= OFS=: file1 >file10

#cat file10
cn=denis.vezina@dom.com,ou=work,o=dom.com:denis.vezina@dom.com:vezina:denis:denis.vezina@dom.com:den is vezina
cn=jean.paul@dom.com,ou=work,o=dom.com:jean.paul@dom.com:Paul:jean:jean.paul@dom.com:jean paul
cn=john.marc@dom.com,ou=work,o=dom.com:john.marc@dom.com:Marc:john:john.marc@dom.com:john marc

Second step, I run this command :

awk 'BEGIN { FS=OFS=":"} {str = $1 ; getline < "file10"; $7 = str}1' file2

cn=denis.vezina@dom.com,ou=work,o=dom.com:denis.vezina@dom.com:vezina:denis:denis.vezina@dom.com:den is vezinahysician
cn=jean.paul@dom.com,ou=work,o=dom.com:jean.paul@dom.com:Paul:jean:jean.paul@dom.com:jean paul:technician
cn=john.marc@dom.com,ou=work,o=dom.com:john.marc@dom.com:Marc:john:john.marc@dom.com:john marc:doctor

What do you think about this solution? have you easiest solution?

THanks a lot for your great help ,

Haydar
 
Old 08-03-2008, 01:10 AM   #21
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
If it works, and is good enough, move on! :-)
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
deleting a field using awk jkeertir Linux - Newbie 5 04-13-2008 10:55 PM
Split CSV, field as filename richmur Programming 2 10-24-2006 08:39 AM
awk and change file content Ayman.mashal Programming 4 06-02-2005 06:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 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
Open Source Consulting | Domain Registration