[comp.sys.ibm.pc] CR? LF? CRLF????

jlh@loral.UUCP (The Mad Merkin Hunter) (06/02/88)

I've downloaded some text files from a UN*X machine via arc and xmodem.  I've
got a pseudo-sed script for edlin I want to run on them (thanks Clayton,
thats a real useful tip you posted a few months back) but it turns out that
the lines end in line feed.  Edlin rides the porcelan bus unless it
sees a CR LF combination.  So, is there an easy way in DOS or MKS vi
to automatically do this conversion?

I see a few solutions, all of which stink up the place.

1)  In UN*X run the files through sed before archiving.
2)  In UN*X modify the source to my archiver.
3)  Write a C program on the pc.  (this is my favorite unless it's already
    been done).
4)  Give up on automation and make the changes by hand.  This would take hours.
5)  Make the changes on UNIX with sed, archive, and download.

						Thanks
							Jim

	"Beware of tall, leggy, beautiful women with spoons in their noses"




-- 
Jim Harkins 
Loral Instrumentation, San Diego
{ucbvax, ittvax!dcdwest, akgua, decvax, ihnp4}!ucsd!sdcc6!loral!jlh

jkg@gatech.edu (Jim Greenlee) (06/02/88)

In article <1770@loral.UUCP> jlh@loral.UUCP (The Mad Merkin Hunter) writes:
>Edlin rides the porcelan bus unless it
>sees a CR LF combination.  So, is there an easy way in DOS or MKS vi
>to automatically do this conversion?

The easiest way to do this is with MKS vi. Just vi the file and do a
":wq!". MKS vi automatically converts LF characters to CR/LF pairs
when it writes the file to disk. The trick is that you have to force 
it to write the file (if you don't edit the file somehow, then the 
copy on disk will not be updated if you do a ":wq", hence the "!").

Disclaimer: this works with the 2.2b version of vi.

						Jim Greenlee
-- 
Mr. News (aka Jim "the Screw" Greenlee)                 jkg@gatech.edu
Instructor, School of ICS, Georgia Tech                 ...!gatech!jkg

Tipper Gore Food: "Race with the Devil on Spanish Highway", "Bush in `88"

bobmon@iuvax.cs.indiana.edu (RAMontante) (06/03/88)

jkg@gatech.UUCP (Jim Greenlee) writes:
>jlh@loral.UUCP (The Mad Merkin Hunter) writes:
>>Edlin rides the porcelan bus unless it
>>sees a CR LF combination.  So, is there an easy way in DOS or MKS vi
>>to automatically do this conversion?
>
>The easiest way to do this is with MKS vi. Just vi the file and do a
>":wq!". MKS vi automatically converts LF characters to CR/LF pairs
>when it writes the file to disk. The trick is that you have to force 

You can also get this behavior with microemacs, cse (posted by, um, whatzisname
from Colorado School of Mines), a recent posting of 'ed'...

If you want to write a filter, it's remarkably easy in something like TurboC --
open the file in binary mode so that LF is sufficient to mark a line.  Read
in convienient-size chunks, write to a file that's been opened in text mode,
and the newlines will "automagically" be sent out as CR/LFs.  I expect MSC
and other languages will offer the same binary/text behavior.

-bob,mon
"In this position, the skier is flying in a complete stall..."
-- 
-bob,mon
"In this position, the skier is flying in a complete stall..."

markh@cutter.UUCP (Mark Hebets) (06/03/88)

In article <1770@loral.UUCP>, jlh@loral.UUCP (The Mad Merkin Hunter) writes:
> I've downloaded some text files from a UN*X machine...
>                        So, is there an easy way in DOS or MKS vi
> to automatically do this conversion [LF->CR/LF] ?
> 

Jim - 
If you use kermit to do the download, you can set the transfer 
mode to text instead of binary, and it fixes line endings on 
the fly.  If you have access to a copy of PC-NFS, it includes
the programs DOS2UNIX and UNIX2DOS that will do the conversion 
for you.

The following programs are filters that will work on a Unix system.
Feel free to hack them up.  

-----------------------------------------------------------
/**
 **  MJH 11/16/87
 **
 **  Read in an ASCII text file in Unix format,
 **  change all LF line endings to CR-LF
 **/

#include <stdio.h>

main()
{
int ch;

/* if ch is a LF, write out CR first */
while( (ch=getchar())!=EOF )
   {
   if(ch=='\n') putchar('\015');
   putchar(ch);
   }

exit(0);
}
-----------------------------------------------------
/**
 **  MJH 11/16/87
 **
 **  Read in an ASCII text file in MS-DOS format,
 **  change all CR-LF line endings to LF,
 **  and strip trailing control-Z
 **/

#include <stdio.h>

main()
{
int ch, lastch;

/* initialize lastch */
if( (lastch=getchar())==EOF ) exit(0);

/* if lastch isn't CR in a CR-LF pair, write it out & get next one */
/* note that this will write out CRs, if they're not succeeded by LFs */
while( (ch=getchar())!=EOF )
   {
   if(lastch!='\015' || ch!='\n')
      putchar(lastch);
   lastch = ch;
   }

/* flush lastch, unless it's DOS EOF (control-Z) */
if(lastch!='\032')
   putchar(lastch);

exit(0);
}

-- 
   Mark Hebets, Software Applications Department, Radian Corp.
   PO 201088, Austin, TX  78720     (512)-454-4797
                     sun!texsun!radian!markh
   im4u!ut-sally!ut-emx!juniper/

jlh@loral.UUCP (The Mad Merkin Hunter) (06/04/88)

In article <17210@gatech.edu> jkg@gatech.UUCP (Jim Greenlee) writes:
=In article <1770@loral.UUCP= jlh@loral.UUCP (The Mad Merkin Hunter) writes:
==Edlin rides the porcelan bus unless it
==sees a CR LF combination.  So, is there an easy way in DOS or MKS vi
==to automatically do this conversion?
=
=The easiest way to do this is with MKS vi. Just vi the file and do a
=":wq!". MKS vi automatically converts LF characters to CR/LF pairs
=when it writes the file to disk.
=
=Disclaimer: this works with the 2.2b version of vi.

MKS vi version 2.2 doesn't do this conversion.  I prefer this method,
what I want from an editor is for it to write my file AS IS to the disk
unless I explicitly tell it the changes to make.   Of course, it would be
nice if I could tell it to make this conversion.....

I found another oddity with this editor.  There is a :source command that
allows you to read commands from a file.  So I made up a file with 8
search and replace commands in it, figuring that vi'ing and :source'ing
5 files beats doing things manually.  Hey, guess what?  As soon as
a search string is not found it aborts execution of the source'd file!
Unfortunatly, not all conversions need be done to all files.  At this
point I uploaded things back to the vax, made my changes with sed,
stuck a bloody CR at the end of each line, and re-downloaded the mess.
God I love DOS.


							Jim
		"I'll never quit drinking.  Everybody hates a quitter."

-- 
Jim Harkins 
Loral Instrumentation, San Diego
{ucbvax, ittvax!dcdwest, akgua, decvax, ihnp4}!ucsd!sdcc6!loral!jlh

wheels@mks.UUCP (Gerry Wheeler) (06/04/88)

In article <1770@loral.UUCP>, jlh@loral.UUCP (The Mad Merkin Hunter) writes:
> I've downloaded some text files from a UN*X machine via arc and xmodem. 
> but it turns out that the lines end in line feed.  Edlin rides the
> porcelan bus unless it sees a CR LF combination.  So, is there an easy
> way in DOS or MKS vi to automatically do this conversion?

Yes. Read the file into vi and write it out.

In fact, most of the MKS tools will read the files which have only
linefeeds at the line ends, and will rewrite the file with cr-lf.  For
example, you can run sed with just the p command.  The exceptions are
cat and tr, which read and write the file in binary mode. 

-- 
     Gerry Wheeler                           Phone: (519)884-2251
Mortice Kern Systems Inc.               UUCP: uunet!watmath!mks!wheels
   35 King St. North                             BIX: join mks
Waterloo, Ontario  N2J 2W9                  CompuServe: 73260,1043

dalegass@dalcsug.UUCP (Dale Gass) (06/07/88)

Jim Harkins writes.
.
. [discussion of MKS vi as a tool to add CR's to a LF delimited file]
.
>I found another oddity with this editor.  There is a :source command that
>allows you to read commands from a file.  So I made up a file with 8
>search and replace commands in it, figuring that vi'ing and :source'ing
>5 files beats doing things manually.  Hey, guess what?  As soon as
>a search string is not found it aborts execution of the source'd file!
>Unfortunatly, not all conversions need be done to all files.  At this
>point I uploaded things back to the vax, made my changes with sed,
>stuck a bloody CR at the end of each line, and re-downloaded the mess.
>God I love DOS.

I imagine the 'source' command's behavior is simply based upon unix's vi
:source behavior.  MKS vi behaves exactly as unix vi in all aspects (I
can't find one discrepancy).

Why upload to the vax to used SED?  You should get SED for the PC.  
GNU's version of SED is public domain, sources are available, and it
seems quite complete...

As far as a simple way of adding CR's to a LF delimited file, most C
compilers stdio library will read LF delimited files properly, and write
them out CRLF delimited (as long as you don't select file type of binary)...
So all you need to build a pipe command to add cr's is a program such as
the following:

#include <stdio.h>
main()
{
    char line[255];

    while (NULL != gets(line)) puts(line);
}

Just compile this to ADDCR.EXE or ADDCR.COM, and then:

C:\> addcr <infile >outfile 
or
C:\> type infile | addcr

Will nicely add CR's to the file...

-uunet!watmath!dalcs!dalegass@dalcsug.uucp

larry@focsys.UUCP (Larry Williamson) (06/09/88)

In article <480@dalcsug.UUCP> dalegass@dalcsug.UUCP (Dale Gass) writes:
>Jim Harkins writes.
>.
>. [discussion of MKS vi as a tool to add CR's to a LF delimited file]
>.

If you have the mks vi editor, but you need to use sed to to do batch
processing, why not use mks' sed editor rather than some other gnu, vax
or some such other editor?

larry



-- 
Larry Williamson                      Focus Automation Systems
UUCP: watmath!focsys!larry    608 Weber St. N, Waterloo, Ontario N2V 1K4
                                          +1 519 746 4918

ejablow@dasys1.UUCP (Eric Robert Jablow) (06/18/88)

The best methhod I know is to use Zmodem, not Xmodem.  Omen Technologies'
`dsz' program, available on many BBSes and on comp.binaries.ibm.pc, etc.
comes with the Xmodem, Ymodem, and Zmodem for both MS-DOS and UNIX.  
Then, to download a text file from UNIX to MS-DOS, just use the command:

		sz -a file1 file2 file3 ...

and to upload from DOS to UNIX, just use the command:

		dsz sz -a file1.txt file2.txt file3.txt ...

Dsz has many other abilities too, especially if you register it.  I love it.
(I have no commercial connjection with Omen--I'm just a satisfied customer.)
-- 
Eric Jablow                      {allegra,philabs,cmcl2}!phri\
Big Electric Cat Public Unix           {bellcore,cmcl2}!cucard!dasys1!ejablow
New York, NY, USA	 	 Soon to be eric@fawn.sb.edu.
Copyright 1988 First Category Press