Board logo

标题: [文本处理] grep 2.22 单文件回车换行版,支持PCRE [打印本页]

作者: CrLf    时间: 2015-12-23 04:21     标题: grep 2.22 单文件回车换行版,支持PCRE

本帖最后由 CrLf 于 2016-1-3 19:25 编辑

单文件无 .dll 依赖
支持 PCRE 正则(-P 开关)
支持 win 下的回车换行

已更新到 Batch-CN
下载地址:http://www.bathome.net/s/tool/?d ... Fgrep&keymode=3
该版本和其他版本见列表:http://www.bathome.net/s/tool/?key=grep&keymode=3

20151223 通过重定义 fwrite 的方式实现支持 \r\n 格式的换行,效率可能比原版略有降低,也未考虑超大 size 的情况
20160103 重定义 fputs/puts/fputc,使其支持 \r\n 格式的换行
实现代码:
  1. size_t _fwrite_CrLf(const void* buffer, size_t size, size_t count, FILE* stream)
  2. {
  3. size_t ret=0;
  4. size_t sc,pc;
  5. char *p,*pbuffer,*q=buffer,lastC='\0';
  6. pc=sc=size*count;
  7. p=pbuffer=malloc(sc*2);
  8. for(;sc-->0;p++,q++){
  9. if(*q=='\n'&&lastC!='\r'){
  10. *p='\r';
  11. p++,pc++;
  12. }
  13. lastC=*p=*q;
  14. }
  15. *p='\0';
  16. ret=fwrite(pbuffer, 1, pc, stream);
  17. free(pbuffer);
  18. return ret;
  19. }
  20. int _fputs_CrLf(const void* buffer, FILE* stream)
  21. {
  22. size_t count, ret;
  23. count = strlen(buffer);
  24. ret = _fwrite_CrLf(buffer, 1, count, stream);
  25. if(ret != count) return ret;
  26. fputc('\r', stream);
  27. fputc('\n', stream);
  28. return ret+2;
  29. }
  30. int _fputc_CrLf(const char chr, FILE* stream)
  31. {
  32. if(chr=='\n')
  33. return fwrite("\r\n", 1, 2, stream) == 2 ? chr : -1;
  34. else
  35. return fputc(chr, stream);
  36. }
  37. #undef fwrite
  38. #define fwrite(buffer, size, count, stream) _fwrite_CrLf(buffer, size, count, stream)
  39. #undef fputs
  40. #define fputs(buffer, stream) _fputs_CrLf(buffer, stream)
  41. #undef puts
  42. #define puts(buffer) _fputs_CrLf(buffer, stdout)
  43. #undef fputc
  44. #define fputc(chr, stream) _fputc_CrLf(chr, stream)
复制代码

作者: 523066680    时间: 2015-12-23 09:51

难怪ID 叫 CrLf
作者: CrLf    时间: 2015-12-23 15:36

回复 2# 523066680


    CrLf 只是缩写,其实我的全名是:
NulSohStxEtxEotEnqAckBelBsHtLfVtFfCrSoSiDleDc1Dc2Dc3Dc4NakSynEtbCanEmSubEscFsGsRsUs
作者: 523066680    时间: 2015-12-23 16:35

回复 3# CrLf


    噢,谢特。
作者: aa77dd@163.com    时间: 2015-12-25 00:00

本帖最后由 aa77dd@163.com 于 2015-12-25 04:15 编辑

回复 3# CrLf

大冬天的, 木太阳, 更难看到彩虹啊



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




无聊弄一 cmd 生成 Discuz 彩虹代码
  1. @echo off & title discuz_RAINBOW_CODE_gen_彩虹代码生成器.cmd
  2. setlocal enableDelayedExpansion
  3. set "pnt="
  4. set /a "sum=0"
  5. for %%a in (00 2A 54 7E A8 D2 FC) do (
  6.     if not "!pnt!"=="" (
  7.         set "P!pnt!=%%a"
  8.         set "N%%a=!pnt!"
  9.     )
  10.     set "pnt=%%a"
  11.     set /a "sum+=1"
  12. )
  13. set /a "sumColor = (sum - 1) * 5 + 1"
  14. set        "R=FC"
  15. set        "G=00"
  16. set        "B=00"
  17. set       "co=RGB"
  18. set  "ALLMODE=PN"
  19. set     "mode=P"
  20. set    "bound=PFCN00"
  21. set   "act_ci=1"
  22. set   "act_ci=2"
  23. set        "B=2A"
  24. set     "mode=N"
  25. REM 行,列间距比
  26. set "r_R2C=14 / 4"
  27. set "r_C2R=4 / 14"
  28. set /a "top_compensation = 0"
  29. set /a "thick = sumColor * %r_R2C%, rad = thick * 2 / 8, t = (rad_max = thick + rad) + 1, r_sq_max = t * t + top_compensation"
  30. set /a "margin = 2, wid = (rad_max + margin) * 2, hei = rad_max * 1 / 2, L_hei = hei * %r_C2R%, xc = wid / 2, yc = rad_max + margin"
  31. >discuz_RAINBOW_CODE_彩虹代码.txt cd.
  32. set /a "radius=rad_max"
  33. for /L %%i in (1 1 %sumColor%) do (
  34.     for %%j in (!act_ci!) do set "act_c=!co:~%%j,1!"
  35.     for %%c in (!act_c!)  do set "cv=!%%c!"
  36.     for %%m in (!mode!!cv!) do if /i not "!bound:%%m=!"=="%bound%" (
  37.         for %%n in (!mode!) do set "mode=!ALLMODE:%%n=!"
  38.         set /a "act_ci = (act_ci - 1) %% 3"
  39.         for %%j in (!act_ci!) do set "act_c=!co:~%%j,1!"
  40.         for %%c in (!act_c!) do set "cv=!%%c!"
  41.     )
  42.     for /f "tokens=1-2" %%a in ("!mode! !cv!") do set "!act_c!=!%%a%%b!"
  43.     set /a "@%%i = r_sq_min = radius * radius, radius -= 1"
  44.     if %%i equ 15 set /a "@15 += 0" & rem 紫区补偿
  45.     set "C_!@%%i!=!R!!G!!B!"
  46. )
  47. set r_sq
  48. set C_
  49. set @
  50. set "exp=0"
  51. for /L %%i in (%sumColor% -1 1) do (
  52.     set "exp=t=x-@%%i>>31,~t&@%%i|t&(!exp!)"
  53. )
  54. set exp
  55. for /L %%y in (0 1 !L_hei!) do (
  56.     set "wh="
  57.     for /L %%x in (0 1 !wid!) do (
  58.         set /a "dx = %%x - xc, dy = (%%y * %r_R2C%) - yc, sq_d = dx * dx + dy * dy"
  59.         set "c="
  60.         if !sq_d! lss !r_sq_min! (
  61.             set "c=white"
  62.         ) else if !sq_d! geq !r_sq_max! (
  63.             set "c=white"
  64.         )
  65.         if not "!c!"=="white" (
  66.             set /a "x=sq_d,cpnt=(%exp%)"
  67.             for %%p in (!cpnt!) do set "c=#!C_%%p!"
  68.             if not "!wh!"=="" (
  69.                 >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=white]!wh![/color]"
  70.             )
  71.             >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=!c!].[/color]"
  72.             REM >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=!sq_d!/!c! "
  73.             set "wh="
  74.         ) else (
  75.             set "wh=!wh!."
  76.         )
  77.     )
  78.     REM if not "!wh!"=="" (
  79.         REM 最后的空白可省略
  80.         REM >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=white]!wh![/color]"
  81.     REM )
  82.     >>discuz_RAINBOW_CODE_彩虹代码.txt echo;
  83. )
  84. start "" discuz_RAINBOW_CODE_彩虹代码.txt
  85. pause
  86. exit
复制代码

作者: 依山居    时间: 2015-12-25 04:47

回复 2# 523066680


    http://www.bathome.net/thread-36622-1-1.html **疗法。
作者: CrLf    时间: 2015-12-25 05:29

本帖最后由 CrLf 于 2015-12-25 05:41 编辑

回复 5# aa77dd@163.com


咱好像跑题了


.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
  1. @echo off & setlocal EnableDelayedExpansion
  2. set /a n1=400,n2=250
  3. set /a Width=n1,Height=n1
  4. set /a Lines=350
  5. set /a EndLine=Lines-Height
  6. set "Count=0"
  7. for /l %%b in (0 4 %Width%) do (
  8. set /a r1=%%b*%%b-n1*n1
  9. if !r1! leq 0 (set "inBigCycle=1") else set "inBigCycle=0"
  10. set /a r2=%%b*%%b-n2*n2
  11. if !Count!==0 set /a rMin=r[0]=!r2!
  12. if !r2! geq 0 if !inBigCycle!==1 (
  13. set /a Count+=1
  14. for %%z in (!Count!) do set "r[%%z]=!r2!"
  15. set "rMax=!r2!"
  16. )
  17. )
  18. set /a "partLen=Count/7,p1=partLen+=^!partLen,p2=2*Count/7,p3=3*Count/7,p4=4*Count/7,p5=5*Count/7,p6=6*Count/7,p7=7*Count/7,partLen+=^!partLen"
  19. set /a p1+=!!(p7-p6-p1)*!!(p2-p1)
  20. for /l %%z in (0 1 !Count!) do (
  21. set /a R=G=B=0
  22. if %%z lss !p1! set /a "R=255-255*(%%z)/(p1)"
  23. if %%z geq !p3! if %%z lss !p4! set /a "R=(255*(%%z-p3)/(partLen))"
  24. if %%z geq !p4! set /a "R=255"
  25. if %%z leq !p1! if %%z lss !p2! set /a "G=255*(%%z)/(p1)"
  26. if %%z geq !p1! if %%z lss !p2! set /a "G=255*(%%z-p1)/(p1)"
  27. if %%z geq !p2! if %%z lss !p4! set /a "G=255"
  28. if %%z geq !p4! if %%z lss !p7! set /a "G=255-255*(%%z-p4)/(!p3!)"
  29. if %%z lss !p2! set /a "B=255"
  30. if %%z geq !p2! if %%z lss !p3! set /a "B=255-255*(%%z-p2)/(partLen)"
  31. set /a n=R*256*256+G*256+B
  32. cmd /c "exit /b %%n%%"
  33. set "RGB[%%z]=#!=ExitCode:~-6!"
  34. )
  35. (for /l %%a in (-%Height% 17 %EndLine%) do (
  36. set "ubb="
  37. for /l %%b in (-%Width% 4 %Width%) do (
  38. set "fontcolor="
  39. set /a r2=%%a*%%a+%%b*%%b-n2*n2
  40. if !r2! geq !rMin! if !r2! leq !rMax! (
  41. for /l %%z in (0 1 !Count!) do (
  42. if !r[%%z]! leq !r2! set nnn=%%z&set "fontcolor=!RGB[%%z]!"
  43. )
  44. )
  45. if not defined fontcolor set "fontcolor=white"
  46. set "ubb=!ubb![color=!fontcolor!].[/color]"
  47. )
  48. echo;!ubb!
  49. )) >ubb.txt
  50. pause
复制代码

作者: xp3000    时间: 2016-1-23 17:42

这个正则表达式批量替换字符串怎么写?大量txt或log,放目录执行

碰到括号数字跟着有顿号的加回车换行
下面顿号是字符串
(1)、(2)、(3)、
......
替换成
(1)、回车换行(2)、回车换行(3)、回车换行
......
替换后、后面要有换行

然后把多个连续回车换行的替换成为1个回车换行
作者: codegay    时间: 2016-1-23 19:18

回复 8# xp3000


    sed fr 这些工具支持通配符*.txt
fr 支持枚举子目录。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2