[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4821
  • Last Modified:

Remove carriage return characters when using Perl module: Spreadsheet::WriteExcel

I'm using the following Perl script to create a spreadsheet from a text file. Basically the script works, but it's inserting a little square(carriage return character) as part of the $ACTIVITY field.


use strict;
use Spreadsheet::WriteExcel;

my $InputFile = "/nmltest/dailyops/data/Log.txt";
my $OutputFile = "//ho/dfs01/work/IS/Operations/GROUP0K/Automation/DailyActivities/Activities/Daily Activities test.xls";

open (INPUT, "$InputFile") or die "$InputFile $!";

my $workbook = Spreadsheet::WriteExcel->new($OutputFile);
my $worksheet = $workbook->add_worksheet();
$worksheet->write('A1', "LAN ID");
$worksheet->write('B1', "Activity");

my $row = 1;

while (<INPUT>)
my ($LANID, $ACTIVITY) = split(/\|/, $_);
$worksheet->write($row, 0, $LANID);
$worksheet->write($row, 1, $ACTIVITY);
close INPUT;

Richard Kreidl
Richard Kreidl
1 Solution
Do every single one of them have it?

If so, try adding after this line:
my ($LANID, $ACTIVITY) = split(/\|/, $_);
$ACTIVITY =~ s/.$//;
Richard KreidlSoftware DeveloperAuthor Commented:
Great that did it!!
If all lines end in "\r\n" then if you set $/="\r\n", the chomp would remove them both
if not all lines end that way, then s/\r$// would remove the last character only if the line ended with \r
you could also use  s/\s+$// to remove all whitespace at the end
my ($LANID, $ACTIVITY) = split(/\s*$|\|/,);
would not get whitespace at the end into  $ACTIVITY) at all
you could also define a :crlf lI/O ayer on the open

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now