删除列中的回车符 [英] Removing Carriage Returns in a column

查看:35
本文介绍了删除列中的回车符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Unix 中有一个文件,该文件在特定字段中出现了一个额外的回车符,我想将其删除.

I have a file in in Unix which has an additional Carriage Return character appearing in a particular field and want to remove it.

我尝试打印字段中字符的 ASCII 值,结果如下:

I tried printing the ASCII value for the characters in the field and it appears as follows :

head -1 BVP.csv |cut -d "," -f26 |tr -d "\n" |od -An -t dc34 78 13

head -1 BVP.csv | cut -d "," -f26 | tr -d "\n" | od -An -t dC 34 78 13

字段中的实际值为:N[回车]

Actual values in the field is: "N[Carriage Return]

所以我尝试如下删除回车符(ASCII 值:13)并尝试将输出打印到新文件 BVP1.csv:

So I tried removing the carriage return (ASCII value :13) as follows and tried printing the output to a new file, BVP1.csv:

tr -d '\r' BVP1.csv

tr -d '\r' < BVP.csv > BVP1.csv

然后我执行了相同的命令

Then I executed the same command

head -1 BVP1.csv |cut -d "," -f26 |tr -d "\n" |od -An -t dc

head -1 BVP1.csv | cut -d "," -f26 | tr -d "\n" | od -An -t dC

34 78

它打印没有回车符的 ASCII 值.

It prints the ASCII values without the Carriage Return.

但是当我在任何文本编辑器甚至从 Windows 中打开文件时,我可以看到该行在文件中分成了一个新记录,即附加的换行符没有被删除.

But when I open the file in any text editor or even from Windows, I can see that the line breaks into a new record in the file, ie, the additional line feed is not removed.

任何人都可以提出一种方法来删除出现在该字段中的这个额外的回车.

Can anyone please suggest a method to remove this additional Carriage return appearing in the field.

提前致谢,汤姆

推荐答案

我敢打赌,在 CR (ASCII 13) 你试图消除.你没有看到它是因为你的 tr -d '\n'.我敢打赌,您需要从 BVP.csv 的字段 26 中删除 CRLF.但是,您只想删除出现在 CR 之后的 LF.

I'm willing to bet that there's an LF (ASCII 10) character after the CR (ASCII 13) you're trying to eliminate. You're not seeing it because of your tr -d '\n'. I'm willing to bet you need to remove both the CR and LF from field 26 of your BVP.csv. But, you only want to remove the LFs that appear just after a CR.

您可以尝试使用这个简单的 perl 方法从您的 csv 文件中去除所有 CR+LF 组合,同时保留完整的 LF:

You might try this simple perl recipe to strip all CR+LF combos from your csv file, while leaving bare LF intact:

perl -p -e 's/\r\n//g' < BVP.csv > BVP1.csv

这将去除 CR+LF 而留下 LF.

This will strip CR+LF while leaving LF alone.

我认为这可以满足您的要求的原因是没有尾随 LF 的裸 CR 相对不常见.MacOS X 之前的文本文件格式使用 CR 而没有 LF.自 OS X 以来,它们的受欢迎程度急剧下降.

The reason I think this will do what you want is that bare CR without a trailing LF are relatively uncommon. The pre-MacOS X text file formats used CR without LF. Since OS X, those have waned in popularity dramatically.

这篇关于删除列中的回车符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆