[CentOS] remove newlines / perl /concise example
ken
gebser at mousecar.comThu Dec 30 21:57:46 UTC 2010
- Previous message: [CentOS] recursively count the words occurrence in the text files
 - Next message: [CentOS] remove newlines / perl /concise example
 - Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
 
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)
  - Previous message: [CentOS] recursively count the words occurrence in the text files
 - Next message: [CentOS] remove newlines / perl /concise example
 - Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
 
More information about the CentOS mailing list