开发者

Removing line feeds or carriage returns from excel with perl

whenever I try a read a row in a .csv file it stops treating the row as a single row and stores it in multiple arrays, as supposed to just one. What i'm seeing is as I read a .csv file it seems to containg some carriage returns/ these weird squares when i open it in notepad. I want to remove these characters from the开发者_高级运维 file so that i can properly read the .csv without it exiting too early. How would I go about doing this with a perl script.


You can clean up trailing whitespace with the chomp() function in perl. You can use something like this to chomp all trailing whitespace and replace it with a single newline:

perl -ne 'while(chomp) { } print "$_\n";' filename.csv


If you wish to read the whole file as one line, simply disable the input record separator:

local $/;
my $file = <>;

That is, if you wish to use the data inside perl. If you simply wish to change the input file for other programs to use, and you do not care about the line feeds/carriage returns:

perl -pi.bak -we 's/[\r\n]+//g' input.csv

This will do an in-place edit of input.csv, and save a backup in input.csv.bak. Be aware that if you run this command twice, the backup is overwritten, so save a proper backup somewhere else.

I would only recommend this if you know that these symbols should not be there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜