LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-21-2006, 04:51 AM   #1
Grafbak
Member
 
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61

Rep: Reputation: 15
Removing quotes from a CSV file with Perl


Dear all,

I am trying to remove some quotes from a specific string in a CSV file. (That came from the very helpfull M$ application Excel who loves to put quotes around anything that seems to be text).
The string is called $ArticleName and contains names like :

"Mounting bracket for 12"" wheel"
and
Mounting bracket 24"
and
"biba Kontiki 22"""
and
"montageplaat 12"" rechts"

This should be printed in the outcome as :
"Mounting bracket for 12" wheel"
and
"Mounting bracket 24""
and
"biba Kontiki 22""
and
"montageplaat 12" rechts"

i have written a perl script to convert the CSV file to a format that i can import in another system. All the values, like price, article_numbers etc are between quotes.

So i tried and thought about the rules for removing quotes and came up with the following :

1) If the first character of $ArticleName is a double-quote, it should be removed.
2) If [0-9]inside $ArticleName is followed directly by 2 double quotes, the last double quote in this sequence should be removed.
OR
[0-9]inside $ArticleName can only be followed by one double quote max.

This is what i have tried so far :

$_=$ItemName;
tr/([0-9]\")//d;
$ItemName=$_;
print " \"$ArticleName\" "; # I need the outer quotes

I tried different tricks with regular expressions, but i can't get the pattern that i want to define correctly and i need some help with that.

If anybody can tell me how to define a regex in Perl that matches 1 or 2 digits followed by one double quote but no more than that, you would make me a happy man. (And i can sleep this weekend).

Thank you in advance.
 
Old 07-21-2006, 05:08 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
how about:
Code:
 s/(\d+")"/$1/g
Incidentally, with excel I have had more success with TAB delimited files.

Last edited by bigearsbilly; 07-21-2006 at 05:09 AM.
 
Old 07-21-2006, 05:31 AM   #3
Grafbak
Member
 
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61

Original Poster
Rep: Reputation: 15
Thank you for your suggestion, however it is still "doing it".
I still get a lot of quotes following one another.

I already found another, less elegant solution.
In the Excel file i replace all the quotes by the word inch.
Removing the problem instead of solving it is not my choice by heart, but im a bit under time pressure here.

I think i'll return back to this issue when the project is done.
Thank you for your help nonetheless.
 
Old 07-21-2006, 06:03 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
how about?
Code:
billym.primadtpdev>perl -pe 's/(\d+")"(?!\s*$)/$1/' 1
"Mounting bracket for 12" wheel"
and
"Mounting bracket 24""
and
"biba Kontiki 22""
and
"montageplaat 12" rechts"
 
Old 07-24-2006, 04:35 PM   #5
Grafbak
Member
 
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61

Original Poster
Rep: Reputation: 15
Thank you Billy,

sorry i couldnt reply sooner. Your tip was most helpfull, but it took me ages to figure out what it actually did.
I figured it out and it works correctly. Thanks again !
 
Old 07-25-2006, 04:23 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
no probs
 
  


Reply


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
Perl, Removing file from path to get directory only xemous Programming 8 04-11-2012 12:13 PM
Parsing XLS or CSV in Perl - what and what not me_the_apprentice Programming 13 02-28-2006 04:44 AM
CSV File AMMullan Programming 2 11-10-2003 01:49 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
perl DBD::CSV pk21 Programming 0 09-26-2003 05:37 AM


All times are GMT -5. The time now is 08:43 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration