remove newlines / perl /concise example

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



Thanks for the previous tips and suggestions.  Here's a more concise
example:

Input file:

<aaaa>
<bbbb>
<cccc>
<dddd>

I want everything on one line, i.e., remove all newlines.  Like so:

<aaaa><bbbb><cccc><dddd>

Simple perl code:

#!/usr/bin/env perl
# Remove newlines from a file in two ways:
# (1) Just "chomp" them;
# (2) Replace them with a space.


# Open input file for reading only.
my $infilename = "/tmp/newline-test-in";
open(my $in, "<", "$infilename") or die "Can't open file
\"$infilename\": $!";

# Open output file for writing only.
my $outfilename = "/tmp/newline-test-out";
open (my $out, ">", "$outfilename") or die "Can\'t open $outfilename for
writing. $!";

# Comment out either, both, or neither of the below "binmode" == no joy
#binmode $in;	
binmode $out;

while (<$in>)
{
# Neither of the two commands below works as expected.
	chomp;			# remove trailing newline.
#	s/\n/ /;		# replace newline with space
	print $out $in;
}

# Close input file.
close $in or die "Error closing $in: $!";

# Close output file.
close $out or die "Error closing output file $out: $!";

### end of perl script

Run this, commenting in/out whatever, and output file is consistently this:

GLOB(0x8ad3c28)GLOB(0x8ad3c28)GLOB(0x8ad3c28)GLOB(0x8ad3c28)


_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux