dos2unix, unix2dos 對應指令

dos2unix, unix2dos 用在 DOS <=> UNIX text file 轉換
DOS 格式 0d 0a
UNIX 格式 0a
有時手邊無此工具, 可用功能相同的指令組合
dos2unix:
sed -i” "s/\r//" file
or
cat file | col -b > newfile
or
cat file | tr -d "\r" > newfile
cat file | tr -d "\015" > newfile

unix2dos:
sed -i” "s/$/\r/" file
sed -i” "s/$/\x0d/" file

-i后面的是单引号组成

以上適用 GNU sed, FreeBSD 下的 sed 不適用

查找至::http://pank.org/blog/archives/000209.html

4.1 unix2dos.c程序模块

  unix2dos.c程序模块:用来把UNIX格式的文件转换为DOS/WINDOWS格式的文件。
  unix2dos.c程序模块源代码如下所示:
/* $Date: 2001/03/28 12:19:21 $
 *
 * UNIX to DOS text format conversion.
 *
 * $Log: unix2dos.c,v $
 * Revision 1.1 2001/03/28 12:19:21 vong
 * Initial revision
 * Written by Haowen Huang
*/
static const char rcsid[] =
  "$Id: unix2dos.c,v 1.1 2001/03/28 12:19:21 vong Exp $";

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "file_transfer.h"

/*
 * Convert file named by pathname to DOS
 * text format, on standard output:
*/
int
unix2dos(const char *pathname) {
  int ch;           /* Current input character */
  FILE *in = 0;        /* Input file */

  if ( !(in = fopen(pathname,"r")) ) {
    fprintf(stderr,"Cannot open input file.\n");
    return 2;
  }
  while ( (ch = fgetc(in)) != EOF ) {
    if ( ch == ‘\n’ )
      putchar(‘\r’);
    putchar(ch);
  }
  fclose(in);
  return 0;
}

4.2 dos2unix.c程序模块

  dos2unix.c程序模块:用来把DOS/WINDOWS格式的文件转换为UNIX格式的文件。
  dos2unix.c程序模块源代码如下所示:

/* $Date: 2001/03/28 12:29:37 $
 *
 * The DOS to UNIX text conversion:
 *
 * $Log: dos2unix.c,v $
 * Revision 1.1 2001/03/28 12:29:37 vong
 * Initial revision
 * Written by Haowen Huang
*/
static const char rcsid[] =
  "$Id: dos2unix.c,v 1.1 2001/03/28 12:29:37 vong Exp $";

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "file_transfer.h"

/*
 * Convert file named by pathname, to
 * UNIX text file format on standard output:
*/
int
dos2unix(const char *pathname) {
  int ch;           /* Current input character */
  int cr_flag;         /* True when CR prev. encountered */
  FILE *in = 0;        /* Input file */

  if ( !(in = fopen(pathname,"r")) ) {
    fprintf(stderr,"Cannot open input file.\n");
    return 2;
  }
  cr_flag = 0; /* No CR encountered yet */
  while ( (ch = fgetc(in)) != EOF ) {
    if ( cr_flag && ch != ‘\n’ ) {
      /* This CR did not preceed LF */
      putchar(‘\r’);
    }
    if ( !(cr_flag = ch == ‘\r’) )
      putchar(ch);
  }
  fclose(in);
  return 0;


评论

该日志第一篇评论

发表评论

评论也有版权!