Adding CR/LF

Chad Perrin perrin at apotheon.com
Sun Sep 30 16:34:44 PDT 2007


On Fri, Sep 28, 2007 at 03:09:52PM -0600, Warren Block wrote:
> On Fri, 28 Sep 2007, jhall at vandaliamo.net wrote:
> 
> >The output I would like to see is:
> >test1
> >test2
> >test3
> 
> String manipulation in sh is painful at best.  Any of the scripting 
> languages are better at this.

A Ruby example, almost a direct translation of the sh code:

  #!/usr/local/bin/ruby

  filenames = %w[test1 test2 test3]

  filenames.each { |fn| print file_list << fn }

That'll give output that looks like:

  test1
  test2
  test3

. . . using CRLF line endings.  If you actually just want newlines in the
Unix style, of course, you can just do this instead of the filenames.each
line:

  puts filenames

That'll automatically output each element of the filenames array with a
newline at the end.

In Perl, that code (for the same output) might look something like this:

  #!/usr/local/bin/perl
  use strict;
  use warnings;

  my @filenames = qw(test1 test2 test3);

  for my $fn (@filenames) { print $fn, "\r\n" };

Again, in Perl you can do this more easily if Unix-style newlines are
what you want instead of CRLF.  In the case of Perl, you'd use the -l
option in the shebang line:

  #!/usr/local/bin/perl -l

. . . and replace the for loop with this:

  print for @filenames;

Of course, this is all moot if you actually have a specific need for
using sh instead of Ruby or Perl.  By the way, though, I think the CRLF
characters were backwards in the original post of this thread, which used
"\n\r".  I'm pretty sure it's "\r\n" as I've done it.  I may just be
having a stupid day, though, and be getting them backwards myself.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of others
we should be glad of an Opportunity to serve others by any Invention of
ours, and this we should do freely and generously."


More information about the freebsd-questions mailing list