Linux bash粘贴不保留换行符
我的bash脚本是从file1.txt粘贴的,它将逐行条目粘贴到CSVformat中。例如: File1.txt包含以下文本:Linux bash粘贴不保留换行符,linux,bash,paste,Linux,Bash,Paste,我的bash脚本是从file1.txt粘贴的,它将逐行条目粘贴到CSVformat中。例如: File1.txt包含以下文本: John 21-2-2015 some city "108 Brent Street Ridgewoods sometown somecountry" CSV上载的转换输出应如下所示: John,21-2-2015,some city,"108 Brent Street Ridgewoods sometown somecountry" 但是,我的代码将其转换为: J
John
21-2-2015
some city
"108 Brent Street
Ridgewoods
sometown
somecountry"
CSV上载的转换输出应如下所示:
John,21-2-2015,some city,"108 Brent Street
Ridgewoods
sometown
somecountry"
但是,我的代码将其转换为:
John,21-2-2015,some city,"108 Brent Street Ridgewoods sometown somecountry"
我想保留换行符
我的代码:
paste -sd, file1.txt > file2.csv
您可以尝试下面的Perl命令
$ perl -0777pe 's/(?s)"[^"]*"(*SKIP)(*F)|\n(?!$)/,/g' file
John,21-2-2015,some city,"108 Brent Street
Ridgewoods
sometown
somecountry"
添加
-i
参数以保存对该文件所做的更改。您可以使用GNUawk的FPAT
功能
cat main.awk
BEGIN {
FPAT = "([^\n]+)|(\"[^\"]+\")"
OFS=","
RS=""
ORS="\n"
}
{ $1=$1; print
}
awk -f main.awk <datafile
John,21-2-2015,some city,"108 Brent Street
Ridgewood
sometown
somecountry"
cat main.awk
开始{
FPAT=“([^\n]+)|”(\“[^\”]+\”)
OFS=“,”
RS=“”
ORS=“\n”
}
{$1=$1;打印
}
哎呀,你真的救了我一天。