Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6745531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:12:59+00:00 2026-05-26T12:12:59+00:00

Say I have a normal CSV like # helloworld.csv hello,world,,,please don’t replace quoted stuff

  • 0

Say I have a normal CSV like

# helloworld.csv
hello,world,,,"please don't replace quoted stuff like ,,",,

If I want mysqlimport to understand that some of those fields are NULL, then I need:

# helloworld.mysql.csv
hello,world,\N,\N,"please don't replace quoted stuff like ,,",\N,\N

I got some help from another question — Why does sed not replace overlapping patterns — but note the problem:

$ perl -pe 'while (s#,,#,\\N,#) {}' -pe 's/,$/,\\N/g' helloworld.csv
hello,world,\N,\N,"please don't replace quoted stuff like ,\N,",\N,\N
                                                           ^^

How can I write the regex so it doesn’t replace ,, if they’re between quotes?

FINAL ANSWER

Here’s the final perl I used, thanks to the accepted answer below:

perl -pe 's/^,/\\N,/; while (s/,(?=,)(?=(?:[^"]*"[^"]*")*[^"]*$)/,\\N/g) {}; s/,$/,\\N/' helloworld.csv

That takes care of leading, trailing, and unquoted empty strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:13:00+00:00Added an answer on May 26, 2026 at 12:13 pm

    Assuming that you won’t have escaped quotes, you can make sure that you only replace ,, if it’s followed by an even number of quotes:

    $subject =~ 
        s/,       # Match ,
        (?=,)     # only if followed by another ,
        (?=       # and only if followed by...
         (?:      # the following group:
          [^"]*"  #  any number of non-quote characters, followed by one quote
          [^"]*"  #  the same thing again (even number!)
         )*       # any number of times, followed by
         [^"]*    # any number of non-quotes until...
         $        # end of string.
        )         # End of lookahead assertion
        /,\N/x
        g;
    

    Input:

    foo,,bar,,,baz,"foo,,,oof",zap,,zip
    

    Output:

    foo,\N,bar,\N,\N,baz,"foo,,,oof",zap,\N,zip
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have some situation like this: class Vertex { public: Position position; Normal
Let's say, I have a normal normalized database like this: CREATE TABLE `users` (
Say I have element sequence like below : <div style=margin-bottom:9px;line-height:normal;margin-top:4px></div> <div style=margin-bottom:9px;line-height:normal;margin-top:4px>Something Here</div> <div
Say have normal class in iOS, called A. I'd like to pass this as
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have a class named Frog, it looks like: public class Frog {
Say I have three files (template_*.txt): template_x.txt template_y.txt template_z.txt I want to copy them
Lets say you have an normal song with two layers, one instrumental and another
Let's say we have a class that looks like this: class A { public:
Say I have just a normal view(not strongly typed) and in this view I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.