简体   繁体   English

Shell'tr'命令返回一个新的空行

[英]Shell 'tr' command returns a new empty line

I had a problem with trim command in Unix. 我在Unix中遇到了trim命令的问题。 I had a original.csv file saved from windows excel that I wanted to port to use in Unix. 我有一个从windows excel保存的original.csv文件,我希望移植到Unix中使用。 The content in original.csv saved from windows is like: 从Windows保存的original.csv中的内容如下:

Func,Failing Cycle
rtg_generic_A_A1_N2_cb_TEST_1_sm32,859180
rtg_generic_A_A1_N3_cb_TEST_1_sm32,859180
rtg_generic_A_A3_N5_dw_cb_TEST_1_sm32,788581 

I wanted to trim off the carriage return in it so I put command: 我想在其中修剪回车,所以我把命令:

tr '\r\n' '\n' < original.csv >trimmed.csv

yet the content inside trimmed.csv looks like: 但trimmed.csv里面的内容看起来像:

Func,Failing Cycle

rtg_generic_A_A1_N2_cb_TEST_1_sm32,859180

rtg_generic_A_A1_N3_cb_TEST_1_sm32,859180

rtg_generic_A_A3_N5_dw_cb_TEST_1_sm32,788581

It seems additional empty line is added in between. 似乎在它们之间添加了额外的空行。 May I know why and how I can get rid of this empty line? 我可以知道为什么以及如何摆脱这条空行?

Thanks 谢谢

This command: 这个命令:

tr '\r\n' '\n'` < file

will translate each instance of \\r into \\n as tr translates character by character. \\r每个实例转换为\\n因为tr逐字符翻译。 This will make each line end with 2 \\n . 这将使每一行以2 \\n结尾。

To delete carriage returns, you can use this tr command: 要删除回车符,可以使用以下tr命令:

tr -d '\r' < file

要将行结尾从Windows转换为Unix,您还可以使用dos2unix命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM