Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

regex - Escape CSV file with sed or other command line tool

I have the following CSV, separated by "|".

101|abc|this is desc|2017
102|xyz|"thie is a long desc
des for xyz continue here."|2017

I have two records, 101 and 102. 102 record is divided into 2 lines. How can I used sed or other command line tools to escape the new line character with "/"?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

awk is better tool to handle this.

Assuming you know how many columns are needed in each line. You can use this awk command:

awk -v n=4 -F '|' 'p+NF<n{p+=NF-1; print $0 ""; next} {p=0} 1' file

101|abc|this is desc|2017
102|xyz|"this is a long desc
des for xyz continue here."|2017

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...