Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在Unix中用~~替换新行和^M字符_File_Unix_Replace_Sed_Newline - Fatal编程技术网

File 如何在Unix中用~~替换新行和^M字符

File 如何在Unix中用~~替换新行和^M字符,file,unix,replace,sed,newline,File,Unix,Replace,Sed,Newline,我不擅长unix 我有一个csv文件,我有多个列。其中一列包含新行和^M字符。我需要用~替换两个(即单个单元格值)之间的所有单元格,以便将单元格值视为单个字段。以下是示例文件: "id","notes" "N001","this is^M test. Again test " "N002","this is perfect" "N00345","this is having ^M problem" 我需要这样的文件: "id","notes" "N001","this is~~test.

我不擅长unix

我有一个csv文件,我有多个列。其中一列包含新行和
^M
字符。我需要用
~
替换两个(即单个单元格值)之间的所有单元格,以便将单元格值视为单个字段。以下是示例文件:

"id","notes"
"N001","this is^M
test.

Again test

"
"N002","this is perfect"
"N00345","this is

having ^M
problem"
我需要这样的文件:

"id","notes"
"N001","this is~~test.~~~~Again test~~~~"
"N002","this is perfect"
"N00345","this is~~~~having ~~problem"
因此,整个单元格值可以作为单个字段值读取

我需要在此需求中添加一个案例,其中单元格中的数据包含
(双引号)。在本例中,我们可以识别结尾为逗号的
。以下是更新的案例数据:

"id","notes"
"N001","this is^M
test. "Again test."

Again test

"
"N002","this is perfect"
"N00345","this is

having ^M
problem as it contains "
test"
我们可以保留或删除它。预期产出为:

"id","notes"
"N001","this is~~test. "Again test."~~~~Again test~~~~"
"N002","this is perfect"
"N00345","this is ~~~~having ~~problem as it contains "~~test"
使用
tr

$ tr '<Ctrl>+m' '~'
$tr'+m'~'

您可以使用
sed
命令执行此操作

仅替换“^M”

sed -i 's|^M|~~|g' file_name
编辑: 谢谢你的评论

添加语句以替换“^M和新行”

替换“^M和新行”**

sed -i ':a;N;$!ba;s|^M\n|~~|g' file_name

要在console中获取“^M”,您应该同时按
Cntrl+v+M

尝试使用
sed

sed -i -e 's/^M//g' -e '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
dos2unix file
sed -i '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
sed -i ':a N; s/\n/~~/; $s/"~~"/"\n"/g; ba' file
注意:要输入
^M
,请键入Ctrl+V,然后键入Ctrl+M

运行命令后的文件内容

"id","notes"
"N001","this is~~test.~~~~Again test~~~~"
"N002","this is perfect"
"N00345","this is~~~~having ~~problem"

使用
dos2unix
后跟
sed

sed -i -e 's/^M//g' -e '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
dos2unix file
sed -i '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
sed -i ':a N; s/\n/~~/; $s/"~~"/"\n"/g; ba' file

简短描述

这里的想法是删除每行中不以
结尾的换行符“

有关更多详细信息,请参阅


编辑

有时单元格中的数据本身包含“内部”

使用
sed

sed -i -e 's/^M//g' -e '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
dos2unix file
sed -i '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file
sed -i ':a N; s/\n/~~/; $s/"~~"/"\n"/g; ba' file
运行用于更新案例数据的命令后的文件内容

"id","notes"
"N001","this is~~test. "Again test."~~~~Again test~~~~"
"N002","this is perfect"
"N00345","this is~~~~having ~~problem as it contains "~~test"

使用
perl
一行程序

不仅要更改^M,还要更改引号之间的新行


^M是在unix会话中通过键盘上的CTRL+VCTRL+M获得的

仍然需要~\n在quoteHi之间,我想您的sed命令工作得很好。但它在控制台rater上打印输出,而不是保存在同一文件中。我会使用-I选项吗?请回复。非常感谢。@user3021141是的,您需要使用
-i
选项进行就地替换。更新了我的答案。jkshah,你是unix高手。谢谢。但还有一个问题,当我运行单个sed命令时,我发现文件中的^M还没有被替换。@user3021141不客气,不是这样的,我还在学习。这可能是因为您可能按字面意思键入了
^M
您是否键入了
Ctrl+V
Ctrl+M
?如果出现问题,我建议您使用
dos2unix
选项否我按照您指定的方式尝试打印^M。不用担心,我将尝试第二个选项。不管怎么说,你是优秀的古菊。谢谢