Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 29 Sep 2007 19:54:19 -0600
From:      Chad Perrin <perrin@apotheon.com>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Adding CR/LF
Message-ID:  <20070930015419.GC24643@demeter.hydra>
In-Reply-To: <20070928140159.M29117@wonkity.com>
References:  <21079.67.171.53.31.1191004462.squirrel@admintool.trueband.net> <20070928140159.M29117@wonkity.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Sep 28, 2007 at 03:09:52PM -0600, Warren Block wrote:
> On Fri, 28 Sep 2007, jhall@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."



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070930015419.GC24643>