Cheers,
Peter
Sent via Deja.com http://www.deja.com/
Before you buy.
p...@dcs.gla.ac.uk wrote:
> I'm trying to strip ^M characters from text files without using the
> dos2unix/dos2ux tools that are present on some boxes. It's pretty
> trivial to write a perl script for it, but since I only want to do
> s/\015//g it seems a bit of a hassle to write a script if the right
> syntax can do it from the command line. Can sed do ASCII replacement
> like this?
--
===============================================================================
vincent CARPENTIER | Certum est, quia impossibile.
carpen...@wanadoo.fr |
vincent.C...@ibazar-group.com | Tertullianus
===============================================================================
tr -d "\015" <infile >outfile
Cheers,
Peter
In article <39C9FCB9...@iBazar-group.com>,
Vincent Carpentier <vincent.C...@iBazar-group.com> wrote:
> You can use tr to do the same thing
> tr -d '\015'
>
"Kurt J. Lanza" wrote:
> Why not just use 'tr'?
>
> tr -d "\015" <infile >outfile
in case you want to use sed anyways:
sed 's/.$//' dosfile >unixfile # be careful if it's not crnl!
sed 's/^M$//' dosfile >unixfile # use Ctrl-v Ctrl-M to get the ^M (bash)
Peter
Save files appropriately
cc -o dos2unix dos2unix.c
cc -o unix2dos unix2dos
-----------------------------
dos2unix.c
-----------------------------
/*
* DOS2UNIX.C
*
* Clean out cr/lf combinations in a file but keep it's original
* date/time stamp.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef TRUE
# define TRUE (1)
# define FALSE (0)
#endif
#if (defined MSDOS || defined _MSC_VER)
# define link(x,y) rename (x, y)
# define R_CNTRL "rb"
# define W_CNTRL "wb"
#else
# define R_CNTRL "r"
# define W_CNTRL "w"
#endif
int dos2u ();
struct stat s_buf;
main (argc, argv)
int argc;
char **argv;
{
char *path;
while (--argc>0)
{
if (stat (path=*++argv, &s_buf) != -1)
{
printf ("Dos2Unix: Cleaning file %s ...\n", path);
if (dos2u (path))
{
fprintf (stderr, "Dos2Unix: Problems cleaning file %s\n", path);
}
}
else
{
fprintf (stderr, "Dos2Unix: Can't stat '%s'\n", path);
}
}
/* EWY: Added exit code */
exit (0);
return (0);
}
int
dos2u (path)
char *path;
{
FILE *in, *out;
int ch,
rval = FALSE;
char temppath [16];
struct utimbuf
time_t actime, modtime;
} ut_buf;
strcpy (temppath, "./clntmp");
#if ! (defined MSDOS || defined _MSC_VER)
strcat (temppath, "XXXXXX");
mktemp (temppath);
#endif
if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
return TRUE;
if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
{
fclose (in);
return TRUE;
}
while ((ch = getc (in)) != EOF)
if ((ch != '\015' && ch != '\032') &&
(putc (ch, out) == EOF) )
{
rval = TRUE;
break;
}
if (fclose (in) == EOF)
{
rval = TRUE;
}
if (fclose (out) == EOF)
{
rval = TRUE;
}
ut_buf.actime = s_buf.st_atime;
ut_buf.modtime = s_buf.st_mtime;
if (utime (temppath, &ut_buf) == -1)
rval = TRUE;
if (unlink (path) == -1)
rval = TRUE;
if (rval)
{
unlink (temppath);
return TRUE;
}
if (link (temppath,path) == -1)
{
fprintf (stderr, "Dos2Unix: Problems renaming '%s' to '%s'\n",
temppath, path);
fprintf (stderr, " However, file '%s' remains\n", temppath);
exit (1);
}
/* EWY: duplicate the mode of the original file */
#if ! (defined MSDOS || defined _MSC_VER)
chmod (path, s_buf.st_mode);
#endif
unlink (temppath);
return FALSE;
}
-----------------------------
unix2dos.c
-----------------------------
/*
* UNIX2DOS.C
*
* Convert lf's to crlf combinations in a file but keep it's original
* date/time stamp.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef TRUE
# define TRUE (1)
# define FALSE (0)
#endif
#if (defined MSDOS || defined _MSC_VER)
# define link(x,y) rename (x, y)
# define R_CNTRL "rb"
# define W_CNTRL "wb"
#else
# define R_CNTRL "r"
# define W_CNTRL "w"
#endif
int u2dos ();
struct stat s_buf;
main (argc, argv)
int argc;
char **argv;
{
char *path;
while (--argc>0)
{
if (stat (path=*++argv, &s_buf) != -1)
{
printf ("Unix2Dos: Cleaning file %s ...\n", path);
if (u2dos (path))
{
fprintf (stderr, "Unix2Dos: Problems cleaning file %s\n", path);
}
}
else
{
fprintf (stderr, "Unix2Dos: Can't stat '%s'\n", path);
}
}
/* EWY: Added exit code */
exit (0);
return (0);
}
int
u2dos (path)
char *path;
{
FILE *in, *out;
int ch,
rval = FALSE;
char temppath [16];
struct utimbuf
time_t actime, modtime;
} ut_buf;
strcpy (temppath, "./clntmp");
#if ! (defined MSDOS || defined _MSC_VER)
strcat (temppath, "XXXXXX");
mktemp (temppath);
#endif
if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
return TRUE;
if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
{
fclose (in);
return TRUE;
}
while ((ch = getc (in)) != EOF)
if (((ch == '\012') && (putc ('\015', out) == EOF)) ||
(putc (ch, out) == EOF) )
{
rval = TRUE;
break;
}
if (fclose (in) == EOF)
{
rval = TRUE;
}
if (fclose (out) == EOF)
{
rval = TRUE;
}
ut_buf.actime = s_buf.st_atime;
ut_buf.modtime = s_buf.st_mtime;
if (utime (temppath, &ut_buf) == -1)
rval = TRUE;
if (unlink (path) == -1)
rval = TRUE;
if (rval)
{
unlink (temppath);
return TRUE;
}
if (link (temppath,path) == -1)
{
fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%s'\n",
temppath, path);
fprintf (stderr, " However, file '%s' remains\n", temppath);
exit (1);
}
/* EWY: duplicate the mode of the original file */
#if ! (defined MSDOS || defined _MSC_VER)
chmod (path, s_buf.st_mode);
#endif
unlink (temppath);
return FALSE;
}
<p...@dcs.gla.ac.uk> wrote in message news:8qcs1t$ega$1...@nnrp1.deja.com...
> I'm trying to strip ^M characters from text files without using the
> dos2unix/dos2ux tools that are present on some boxes. It's pretty
> trivial to write a perl script for it, but since I only want to do
> s/\015//g it seems a bit of a hassle to write a script if the right
> syntax can do it from the command line. Can sed do ASCII replacement
> like this?
>
> Cheers,
>
> Peter
>
>
I use a simple shell function for that purpose...
---------------snip---------------
dos2unix=() {
if [ $# -ge 1 ]; then
while [ -n "$1" ]; do
FILE=$1;
shift;
cat $FILE | tr -d '\015' >$FILE.tmp && mv $FILE.tmp $FILE;
done;
else
echo "Usage: \`dos2unix file [file2, file3...]'";
fi
}
---------------snip---------------
Peter
So what's wrong with perl then?
perl -pe 's/\r$//'
> Can sed do ASCII replacement like this?
Yes, but you need to put the Control-M literally into the
command string:
sed 's/^M$//'
One hack to do this which survives usenet posting better:
CR=`echo x | tr x '\015'`
sed "s/$CR\$//'
--Ken Pizzini