LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-30-2014, 11:14 AM   #1
shelby
Member
 
Registered: Nov 2002
Location: Rio Rancho, NM
Distribution: RHEL, CentOS & Ubuntu
Posts: 90

Rep: Reputation: 15
Help with cleaning up a csv file


I am not quite sure how to attack this one. I have a CSV file (see example below). I need to clean up the first column. In the area that has quotes I just want Jsmith, I want the cn= and everything past the Jsmith gone in the quotes. See below.

How my CSV looks:
"cn=Jsmith,ou=Employee,ou=Library,o=Users”,Jsmith2,,,jim@email.com,jim

What I need after clean up
Jsmith,Jsmith2,,,jim@email.com,jim

Thanks...
 
Old 04-30-2014, 11:22 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
This should get you pretty close to what you want. Some extra manipulation will be needed.

Code:
cut -d "," -f 1,5,8,9
 
Old 04-30-2014, 11:47 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
sed -r 's/^[^=]+=([^,]+).*"/\1/'
 
2 members found this post helpful.
Old 04-30-2014, 11:54 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Doesn't work properly on Centos 6.5:

Code:
[root@dev ~]# echo "cn=Jsmith,ou=Employee,ou=Library,o=Users”,Jsmith2,,,jim@email.com,jim" | sed -r 's/^[^=]+=([^,]+).*"/\1/'
cn=Jsmith,ou=Employee,ou=Library,o=Users”,Jsmith2,,,jim@email.com,jim

[root@dev ~]# sed --version
GNU sed version 4.2.1
 
Old 04-30-2014, 12:08 PM   #5
shelby
Member
 
Registered: Nov 2002
Location: Rio Rancho, NM
Distribution: RHEL, CentOS & Ubuntu
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Code:
sed -r 's/^[^=]+=([^,]+).*"/\1/'
This works perfectly...Thank you!!!
 
Old 04-30-2014, 12:10 PM   #6
shelby
Member
 
Registered: Nov 2002
Location: Rio Rancho, NM
Distribution: RHEL, CentOS & Ubuntu
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by szboardstretcher View Post
Doesn't work properly on Centos 6.5:

Code:
[root@dev ~]# echo "cn=Jsmith,ou=Employee,ou=Library,o=Users”,Jsmith2,,,jim@email.com,jim" | sed -r 's/^[^=]+=([^,]+).*"/\1/'
cn=Jsmith,ou=Employee,ou=Library,o=Users”,Jsmith2,,,jim@email.com,jim

[root@dev ~]# sed --version
GNU sed version 4.2.1
I had to update my sed command to 4.2.2 and it worked fine. I am working in OS X so I had to update via MacPorts. Maybe you can update yours via yum?
 
Old 04-30-2014, 01:18 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@szboardstretcher - The issue is you copied the input from the user and the second set of double quotes is actually something else ... not sure what. Also, if you use double quotes around double quotes
you will lose the original form, try:
Code:
echo '"cn=Jsmith,ou=Employee,ou=Library,o=Users",Jsmith2,,,jim@email.com,jim' | sed -r 's/^[^=]+=([^,]+).*"/\1/'
 
1 members found this post helpful.
Old 04-30-2014, 01:20 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Derp. Sorry about that! Yeah, looks real nice now.
 
Old 04-30-2014, 02:49 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
All good .. took me a while to figure out why it wasn't work straight away for me too
 
Old 04-30-2014, 02:54 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Aye. Its been a long day

I eventually just put it into a file as-is and cat'd the file through a pipe to the command. Your command works nicely.
 
  


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
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
Parse a CSV file output to text file beto Linux - Newbie 3 04-25-2012 08:45 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 01:09 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM

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

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