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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-21-2006, 03:51 AM
|
#1
|
|
Member
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61
Rep:
|
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.
|
|
|
|
07-21-2006, 04:08 AM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
how about :
Incidentally, with excel I have had more success with TAB delimited files.
Last edited by bigearsbilly; 07-21-2006 at 04:09 AM.
|
|
|
|
07-21-2006, 04:31 AM
|
#3
|
|
Member
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61
Original Poster
Rep:
|
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.
|
|
|
|
07-21-2006, 05:03 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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"
|
|
|
|
07-24-2006, 03:35 PM
|
#5
|
|
Member
Registered: Jun 2003
Location: /dev/null
Distribution: Knoppix 3.3
Posts: 61
Original Poster
Rep:
|
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 !
|
|
|
|
07-25-2006, 03:23 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
no probs 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:49 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
|
|