tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
From: Werner Rost <werner.rost () boge ! mannesmann ! de>
To: tru64-unix-managers
Subject: SUMMARY: Inserting <carriage return> before <newline>
Date: Wed, 08 Dec 1999 07:46:32 +0000	[thread overview]
Message-ID: <96079091516214.tru64-unix-managers.marc@archive.invalid> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: multipart/mixed; boundary="--Boundary_(ID_yF5dmo5EMwzdaUdHpgJlUQ)", Size: 6081 bytes --]

This is a multi-part message in MIME format.

--Boundary_(ID_yF5dmo5EMwzdaUdHpgJlUQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8BIT

Hi,

the original question was:

>  some unix files crashes PC-applications because each line ends
>  with \n (newline), but should end with \r \n  (carriage return
>  and newline). Has anybody a script (sed ?), that inserts \r
>  before \n ?


Thanks for all the prompt responses. Here are the answers.

-------------------------------------------------------------
Use ftp (asci-transfer-mode)
-------------------------------------------------------------

this works fine!!!


-------------------------------------------------------------
Use sed
-------------------------------------------------------------
#/bin/csh -f
sed -e 's/$/^M/' $1 > $2

...............................................................

Being that as it may:

	sed 's/$/^M/' file > file.Microsoft

where you type the ^M as a control-V and a control-M will do the
trick, just as

	sed 's/^M$//' file.Microsoft > file

will reverse the process.



-------------------------------------------------------------
Use other Unix-tools
-------------------------------------------------------------
awk '{printf("%s\r\n", $0)}' filename.nl > filename.crnl

cat file | awk '{printf $0 "\n\r"}'

tr "\012" "\015" < infile > outfile    <-- this does not work !!


-------------------------------------------------------------
Use c-programs
-------------------------------------------------------------



-------------------------------------------------------------
Use pearl  (we dont have pearl)
-------------------------------------------------------------
perl -pi.bak -e 's/\n/\r\n/' file


#! /usr/bin/perl -wpi.bak
s/$/\r/ ;


#!/usr/local/bin/perl -pi~
s/\n/\r\n/o;

#!/usr/local/bin/perl -pi~
s/\r//o;


#!/usr/local/bin/perl
# file name is c.pl
# this has not been tested Werner!
while (<>)
{
	s/$/
/;
	print $_;

}


-------------------------------------------------------------
Misc.
-------------------------------------------------------------
http://bul.eecs.umich.edu/~crowej/u2d.html   (very nice!)



Mit freundlichen Gr��en
Werner Rost
------------------------------------------------------
Werner Rost, Mannesmann-Boge GmbH
Abt:    KIR
Tel:    0228/3825-420
Fax:    0228/3825-398
Email:  werner.rost@boge.mannesmann.de
Friesdorfer Str. 175, 53175 Bonn - Bad Godesberg
------------------------------------------------------


--Boundary_(ID_yF5dmo5EMwzdaUdHpgJlUQ)
Content-type: application/octet-stream; name=dos2ux.c
Content-disposition: attachment; filename=dos2ux.c
Content-transfer-encoding: quoted-printable

/*  Convert cr-lf (DOS/Windows) to lf (Unix)
 *  line terminators.
 *  Written by Anthony W. Hursh.
 *  Released to the public domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


main(int argc, char *argv[])
{
    FILE *infile, *outfile;
    char tempName[1024];
    int c;
    if(argc !=3D 2)
    {
	    fprintf(stderr,"Usage: dos2unix <textfile>\n");
            exit(1);
    }
    strcpy(tempName,argv[1]);
    strcat(tempName,"$$$");  // Create a new file name by appending $$$ =
to the old
                            //  file name. Hopefully this doesn't =
conflict with
                           //   anything.

    if((infile =3D fopen(tempName,"rb")) !=3D NULL) // It already =
exists, so don't clobber it.
    {
	fprintf(stderr,"Temporary file %s already exists. Please delete =
it.\n",tempName);
	fclose(infile);
        exit(1);
    }
                           =20
    if((infile =3D fopen(argv[1],"rb")) =3D=3D NULL)
    {
           fprintf(stderr,"%s: Can't open.\n",argv[1]);
           exit(1);
    }
    if((outfile =3D fopen(tempName,"wb")) =3D=3D NULL)
    {
           fprintf(stderr,"%s: Can't open.\n",tempName);
           exit(1);
    }
    while((c =3D getc(infile)) !=3D EOF)
    {
          if(c =3D=3D '\r') // Carriage return, don't do anything.
               continue;
          else putc(c,outfile); // Otherwise write the character to the =
new file.
     }
    fclose(infile);
    fclose(outfile);
    unlink(argv[1]); // Delete old file.
    rename(tempName,argv[1]); // Rename new file to old name.
}

--Boundary_(ID_yF5dmo5EMwzdaUdHpgJlUQ)
Content-type: application/octet-stream; name=u2dosx.c
Content-disposition: attachment; filename=u2dosx.c
Content-transfer-encoding: quoted-printable

/*  Convert lf (Unix) to cr-lf (DOS/Windows)
 *  line terminators.
 *  Written by Anthony W. Hursh.
 *  Released to the public domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


main(int argc, char *argv[])
{
    FILE *infile, *outfile;
    char tempName[1024];
    int c;
    if(argc !=3D 2)
    {
	    fprintf(stderr,"Usage: unix2dos <textfile>\n");
            exit(1);
    }
    strcpy(tempName,argv[1]);
    strcat(tempName,"$$$");  // Create a new file name by appending $$$ =
to the old
                            //  file name. Hopefully this doesn't =
conflict with
                           //   anything.

    if((infile =3D fopen(tempName,"rb")) !=3D NULL) // It already =
exists, so don't clobber it.
    {
	fprintf(stderr,"Temporary file %s already exists. Please delete =
it.\n",tempName);
	fclose(infile);
        exit(1);
    }
                           =20
    if((infile =3D fopen(argv[1],"rb")) =3D=3D NULL)
    {
           fprintf(stderr,"%s: Can't open.\n",argv[1]);
           exit(1);
    }
    if((outfile =3D fopen(tempName,"wb")) =3D=3D NULL)
    {
           fprintf(stderr,"%s: Can't open.\n",tempName);
           exit(1);
    }
    while((c =3D getc(infile)) !=3D EOF)
    {
          if(c =3D=3D '\n') // Line feed, replace with carriage =
return/line feed.
          {
		putc('\r',outfile);
		putc('\n',outfile);
	  }
          else putc(c,outfile); // Otherwise write the character to the =
new file.
     }
    fclose(infile);
    fclose(outfile);
    unlink(argv[1]); // Delete old file.
    rename(tempName,argv[1]); // Rename new file to old name.
}


--Boundary_(ID_yF5dmo5EMwzdaUdHpgJlUQ)--

                 reply	other threads:[~1999-12-08  7:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox