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)
On Thu, Dec 30, 2010 at 04:57:46PM -0500, ken wrote:
open(my $in, "<", "$infilename") or die "Can't open file
while (<$in>) { # Neither of the two commands below works as expected. chomp; # remove trailing newline. # s/\n/ /; # replace newline with space print $out $in;
What exactly do you think you're printing here???? $in is the filehandle, not the content. $_ is the content.
print $out $_;
On 30 December 2010 21:57, ken gebser@mousecar.com wrote:
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:
Code like this is often simpler if you use the Unix filter model and let the operating system take care of opening all the files.
#!/usr/bin/env perl
use strict; use warnings;
chomp(my @lines = <>);
print @lines;
If this is in a file called 'join', you can run it like this:
$ ./join < input.txt > output.txt
hth,
Dave...
On Sat, Jan 01, 2011 at 03:42:46PM +0000, Dave Cross wrote:
Code like this is often simpler if you use the Unix filter model and let the operating system take care of opening all the files.
#!/usr/bin/env perl
use strict; use warnings;
chomp(my @lines = <>);
print @lines;
That's one way of using up a lot of memory.
If this is in a file called 'join', you can run it like this:
$ ./join < input.txt > output.txt
Use the tools you've already got as part of the OS install: /usr/bin/tr -d '\012' < input.txt > output.txt
----- Original Message ----- | On Sat, Jan 01, 2011 at 03:42:46PM +0000, Dave Cross wrote: | > Code like this is often simpler if you use the Unix filter model and | > let the operating system take care of opening all the files. | > | > #!/usr/bin/env perl | > | > use strict; | > use warnings; | > | > chomp(my @lines = <>); | > | > print @lines; | | That's one way of using up a lot of memory. | | > If this is in a file called 'join', you can run it like this: | > | > $ ./join < input.txt > output.txt | | Use the tools you've already got as part of the OS install: | /usr/bin/tr -d '\012' < input.txt > output.txt | | -- | | rgds | Stephen | _______________________________________________ | CentOS mailing list | CentOS@centos.org | http://lists.centos.org/mailman/listinfo/centos
You could have used /usr/bin/tr -d "\n" < input.txt > output.txt . It looks a bit cleaner and indicates that no special incantation or knowledge of the ASCII character set is required.