删除带引号字符串中的回车符和换行符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除带引号字符串中的回车符和换行符相关的知识,希望对你有一定的参考价值。

If a flat file contains embedded carriage return (CR) and linefeed characters (LF) inside double quotes, SAS will interpret them as end of line markers. This may cause your file to be read incorrectly.
This example replaces CR/LF characters within double quotes with other printable characters. CR/LF characters outside of double quotes are untouched.

In this sample, the external file is updated in place. You cannot separate the input and output because the code uses shared buffers.

You should make a copy of your file before running this sample on it.
  1. /* Create sample .csv file containing CR/LF bytes. '0D'x is the */
  2. /* hexadecimal representation of CR and '0A'x is the hexadecimal */
  3. /* representation of LF. */
  4.  
  5. data _null_;
  6. file "c:sample.csv";
  7. /* Code the PUT statement so the hex values will be inside the */
  8. /* the double quotes in the output file SAMPLE.CSV. */
  9. put '"field1","field2","field3'
  10. '0D'x
  11. '",'
  12. '"field4","field5","field6'
  13. '0A'x
  14. '",'
  15. '"field7","field8","field9"'
  16. ;
  17. run;
  18.  
  19. /* Read in the test file created above. */
  20. data out;
  21. /* The DSD option assumes the delimiter is a comma. You can override */
  22. /* the delimiter with the DLM= option if needed. */
  23. infile "c:sample.csv" dsd truncover;
  24. length var1 - var9 $15;
  25. input var1 - var9 $;
  26. run;
  27.  
  28. proc print;
  29. title 'File is read incorrectly due to embedded CR/LF';
  30. run;
  31.  
  32. /************************** CAUTION ***************************/
  33. /* */
  34. /* This program UPDATES IN PLACE, create a backup copy before */
  35. /* running. */
  36. /* */
  37. /************************** CAUTION ***************************/
  38.  
  39. /* Replace carriage return and linefeed characters inside */
  40. /* double quotes with a specified character. This sample */
  41. /* uses '@' and '$', but any character can be used, including */
  42. /* spaces. CR/LFs not in double quotes will not be replaced. */
  43.  
  44.  
  45. %let repA='@'; /* replacement character LF */
  46. %let repD='$'; /* replacement character CR */
  47.  
  48.  
  49. %let dsnnme="c:sample.csv"; /* use full path of CSV file */
  50.  
  51. data _null_;
  52. /* RECFM=N reads the file in binary format. The file consists */
  53. /* of a stream of bytes with no record boundaries. SHAREBUFFERS */
  54. /* specifies that the FILE statement and the INFILE statement */
  55. /* share the same buffer. */
  56. infile &dsnnme recfm=n sharebuffers;
  57. file &dsnnme recfm=n;
  58.  
  59. /* OPEN is a flag variable used to determine if the CR/LF is within */
  60. /* double quotes or not. Retain this value. */
  61. retain open 0;
  62.  
  63. input a $char1.;
  64. /* If the character is a double quote, set OPEN to its opposite value. */
  65. if a = '"' then open = ^(open);
  66.  
  67. /* If the CR or LF is after an open double quote, replace the byte with */
  68. /* the appropriate value. */
  69. if open then do;
  70. if a = '0D'x then put &repD;
  71. else if a = '0A'x then put &repA;
  72. end;
  73. run;
  74.  
  75. /* Read in new version of file to check for success. */
  76. data outp;
  77. infile "c:sample.csv" dsd dlm=',' truncover;
  78. length var1 - var9 $ 15;
  79. input var1 - var9 $ ;
  80. run;
  81.  
  82. proc print;
  83. title1 'File is read correctly after transformation ';
  84. title2 "Look for printable characters &repa &repd in variable values ";
  85. run;
  86.  
  87. /* OPTIONAL -- Delete external file */
  88. data _null_;
  89. fname="tempfile";
  90. rc=filename(fname,"c:sample.csv");
  91. if rc = 0 and fexist(fname) then rc=fdelete(fname);
  92. rc=filename(fname);
  93. run;

以上是关于删除带引号字符串中的回车符和换行符的主要内容,如果未能解决你的问题,请参考以下文章

无法替换 php 中的回车符和换行符

如何在sql server 数据库的字段加入回车符和换行符

JSON删除空格和回车,但保留引号中的空格

批处理语句里面换行符或者回车符怎么写?

从文件中删除所有回车和换行

Oracle删除字段中的空格、回车及指定字符的实例代码