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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
08-01-2008, 09:30 AM
#1
Member
Registered: Jul 2008
Posts: 35
Rep:
AWK: change a particular field in a csv file help help help!!!!
Hi,
I have a csv file, it contains 4 columns or fields where these fields are separated by ":"
first fied is cn=10,ou=work,o=dom.com
second field is last name as vezina
third field is first name as denis
fourth field is a mail address as
denis.vezina@dom.com
here is the csv file:
cn=10,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=20,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=30,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=40,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com
I hope to put the mail address instead cn,
example
cn=denis.vezina@dom.com instead cn=10
and I want to get this file result:
cn=denis.vezina@dom.com,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=jean.saul@dom.com,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=isabelle.didi@dom.com,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=alain.kapa@dom.com,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com
I tried this command:
awk -F: -v OFS=: '{$1 = $4; print }' file
but I get this result:
cn=denis.vezina@dom.com:vezina:denis:denis.vezina@dom.com
cn=jean.saul@dom.com:saul:jean:jean.saul@dom.com
cn=isabelle.didi@dom.com:didi:isabelle:isabelle.didi@dom.com
cn=alain.kapa@dom.com:kapa:alain:alain.kapa@dom.com
I loose this data ",ou=work,o=dom.com" in each line.
Is it a way or a command awk/sed/cut to get like this result?
cn=denis.vezina@dom.com,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=jean.saul@dom.com,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=isabelle.didi@dom.com,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=alain.kapa@dom.com,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com
Thanks a lot for your help!
Haydar
08-01-2008, 09:39 AM
#2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Hi,
Is this what you are looking for?
awk -F: -v OFS=: '{$1 = $4; print "cn="$1",ou=work,o=dom.com", $2, $3, $4 }' infile
Only works if the cn= and ,ou=work,o=dom.com are the same for all lines.
Hope this helps.
08-01-2008, 10:22 AM
#3
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
druuna
Hi,
Is this what you are looking for?
awk -F: -v OFS=: '{$1 = $4; print "cn="$1",ou=work,o=dom.com", $2, $3, $4 }' infile
Only works if the cn= and ,ou=work,o=dom.com are the same for all lines.
Hope this helps.
But if other lines contain a data different to ou=work,o=dom.com.
Is there another solution for different domains?
Thanks for this solution.
Haydar
08-01-2008, 10:39 AM
#4
Member
Registered: Jul 2008
Posts: 159
Rep:
Code:
awk -F: -v OFS=: '{sub(/^cn=[^,]+/, "cn=" $4); print;}'
08-01-2008, 10:41 AM
#5
Member
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212
Rep:
Given your original file format from your previous post, you could try something like this with your ldif file:
Code:
awk -F': |\n' '{
sub(/cn=[^,]*/, "cn=" $10, $2)
print $2,$4,$6,$8,$10
}' RS= OFS=: file
08-01-2008, 10:43 AM
#6
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
Quote:
Originally Posted by
burschik
Code:
awk -F: -v OFS=: '{sub(/^cn=[^,]+/, "cn=" $4); print;}'
Good call!
08-01-2008, 11:01 AM
#7
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
burschik
Code:
awk -F: -v OFS=: '{sub(/^cn=[^,]+/, "cn=" $4); print;}'
Hi Burschik,
I ran this command that you provide to me and I got this result:
awk -F: -v OFS=: '{sub(/^cn=[^,]+/, "cn=" $4); print;}' jj.txt
cn=10,ou=work,o=dom.com:vezina:denis:denis.vezina@dom.com
cn=jean.saul@dom.com,ou=work,o=dom.com:saul:jean:jean.saul@dom.com
cn=isabelle.didi@dom.com,ou=work,o=dom.com:didi:isabelle:isabelle.didi@dom.com
cn=alain.kapa@dom.com,ou=work,o=dom.com:kapa:alain:alain.kapa@dom.com
If you check the first line, you will notice that cn=10 and it does not change to mail.
Thanks
08-01-2008, 11:05 AM
#8
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
radoulov
Given your original file format from your previous post, you could try something like this with your ldif file:
Code:
awk -F': |\n' '{
sub(/cn=[^,]*/, "cn=" $10, $2)
print $2,$4,$6,$8,$10
}' RS= OFS=: file
Hi Radoulov,
I ran your command that you provided to me :
awk -F': |\n' '{sub(/^cn=[^,]+/, "cn=" $10, $2) print $2,$4,$6,$8,$10,$12 }' RS= OFS=: book.ldif
and I got this error result:
awk: cmd. line:1: {sub(/^cn=[^,]+/, "cn=" $10, $2) print $2,$4,$6,$8,$10,$12 }
awk: cmd. line:1: ^ parse error
by the way, the book.ldif contains these data:
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:
isabelle.didi@dom.com
sn: didi
givenName: isabelle
mail:
isabelle.didi@dom.com
displayName: isabelle didi
dn: cn=40,ou=work,o=dom.com
cn:
alain.papa@dom.com
sn: papa
givenName: alain
mail:
alain.papa@dom.com
displayName: alain papa
Thanks for your help
08-01-2008, 11:28 AM
#9
Senior Member
Registered: Aug 2006
Posts: 2,697
Code:
awk 'BEGIN{FS=":"}
{
old=FS; a=$NF
FS=","; $0=$0
$1="cn="a; OFS=","
$1=$1;
FS=old
}1' file
08-01-2008, 11:56 AM
#10
Member
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212
Rep:
Quote:
Originally Posted by
haydar68
Hi Radoulov,
I ran your command that you provided to me :
awk -F': |\n' '{sub(/^cn=[^,]+/, "cn=" $10, $2) print $2,$4,$6,$8,$10,$12 }' RS= OFS=: book.ldif
[...]
I did not post this command ... (you're missing the semicolon (or the new line) before the print statement).
Consider this:
Code:
zsh-4.3.4% cat file
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: isabelle.didi@dom.com
sn: didi
givenName: isabelle
mail: isabelle.didi@dom.com
displayName: isabelle didi
dn: cn=40,ou=work,o=dom.com
cn: alain.papa@dom.com
sn: papa
givenName: alain
mail: alain.papa@dom.com
displayName: alain papa
zsh-4.3.4% awk -F': |\n' '{
sub(/cn=[^,]*/, "cn=" $10, $2)
print $2,$4,$6,$8,$10
}' RS= OFS=: file
cn=denis.vezina@dom.com,ou=work,o=dom.com:denis.vezina@dom.com:vezina:denis:denis.vezina@d
om.com
cn=jean.paul@dom.com,ou=work,o=dom.com:jean.paul@dom.com:Paul:jean:jean.paul@dom.com
cn=isabelle.didi@dom.com,ou=work,o=dom.com:isabelle.didi@dom.com:didi:isabelle:isabelle.di
di@dom.com
cn=alain.papa@dom.com,ou=work,o=dom.com:alain.papa@dom.com:papa:alain:alain.papa@dom.com
zsh-4.3.4%
Last edited by radoulov; 08-01-2008 at 12:09 PM .
08-01-2008, 02:13 PM
#11
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
radoulov
I did not post this command ... (you're missing the semicolon (or the new line) before the print statement).
Consider this:
Code:
zsh-4.3.4% cat file
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: isabelle.didi@dom.com
sn: didi
givenName: isabelle
mail: isabelle.didi@dom.com
displayName: isabelle didi
dn: cn=40,ou=work,o=dom.com
cn: alain.papa@dom.com
sn: papa
givenName: alain
mail: alain.papa@dom.com
displayName: alain papa
zsh-4.3.4% awk -F': |\n' '{
sub(/cn=[^,]*/, "cn=" $10, $2)
print $2,$4,$6,$8,$10
}' RS= OFS=: file
cn=denis.vezina@dom.com,ou=work,o=dom.com:denis.vezina@dom.com:vezina:denis:denis.vezina@d
om.com
cn=jean.paul@dom.com,ou=work,o=dom.com:jean.paul@dom.com:Paul:jean:jean.paul@dom.com
cn=isabelle.didi@dom.com,ou=work,o=dom.com:isabelle.didi@dom.com:didi:isabelle:isabelle.di
di@dom.com
cn=alain.papa@dom.com,ou=work,o=dom.com:alain.papa@dom.com:papa:alain:alain.papa@dom.com
zsh-4.3.4%
Hi Radoulov,
sorry for last comment. I tried the same command.
Now I tried it again and I get the same issue:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2) print $2,$4,$6,$8,$10 }' RS= OFS=: file
awk: cmd. line:1: {sub(/cn=[^,]*/, "cn=" $10, $2) print $2,$4,$6,$8,$10 }
awk: cmd. line:1: ^ parse error
Have you any idea what it cause this issue?
Thanks a lot for your help,
Haydar
08-01-2008, 02:15 PM
#12
Member
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212
Rep:
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
Last edited by radoulov; 08-01-2008 at 02:16 PM .
08-01-2008, 02:18 PM
#13
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
ghostdog74
Code:
awk 'BEGIN{FS=":"}
{
old=FS; a=$NF
FS=","; $0=$0
$1="cn="a; OFS=","
$1=$1;
FS=old
}1' file
Hi GhostDog74,
I tested this command and it works fine.
Thanks a lot for your great help,
Haydar
08-01-2008, 02:28 PM
#14
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
radoulov
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,
I tried this command on one line and I get this error message:
awk -F': |\n' '{sub(/cn=[^,]*/, "cn=" $10, $2);print $2,$4,$6,$8,$10 }' RS= OFS=: file
awk: cmd. line:1: (FILENAME=file FNR=1) fatal error: internal error
Aborted
I tried this command on many lines and I get this error message:
awk -F': |\n' '{
> sub(/cn=[^,]*/, "cn=" $10, $2)
> print $2,$4,$6,$8,$10
> }' RS= OFS=: file
awk: cmd. line:2: (FILENAME=file FNR=1) fatal error: internal error
Aborted
cat file
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:
isabelle.didi@dom.com
sn: didi
givenName: isabelle
mail:
isabelle.didi@dom.com
displayName: isabelle didi
dn: cn=40,ou=work,o=dom.com
cn:
alain.papa@dom.com
sn: papa
givenName: alain
mail:
alain.papa@dom.com
displayName: alain papa
Is it related to the Linux version? I use RedHat Enterprise AS 2.1
THanks for your great help,
Haydar
08-01-2008, 05:38 PM
#15
Member
Registered: Jul 2008
Posts: 35
Original Poster
Rep:
Quote:
Originally Posted by
radoulov
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,
I run this command in two ways on RHEL AS4 and it works fine. I think that the previous problem is related to the awk version or the RedHat version 2.1.
THanks,
Haydar
All times are GMT -5. The time now is 11:12 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News