Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

regex - How to Capture Multiple New lines between two characters?

I am trying to match and remove multiple newlines between quotations to clean up a CSV file. SED is what I am most familiar with, but am happy to use whatever assuming I have access to it.

Example

"ABC","This is
a
test
","1","2","This
is
another
test"

Expected End Result

"ABC","This is a test","1","2","This is another test"

I've tried multiple patterns on regex101.com and looked around the "similar questions," but can't seem to find anything remotely close to working. Any help would be appreciated.

question from:https://stackoverflow.com/questions/65878946/how-to-capture-multiple-new-lines-between-two-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may try this gnu awk:

awk -v RS='"[^"]+"' 'RT {gsub(/"
+|
+"/, """, RT); gsub(/
+/, " ", RT)} {ORS=RT} 1' file.csv

"ABC","This is a test","1","2","This is another test"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...