Board logo

标题: [文本处理] 怎么提高文本替换效率与简洁性,这有短代码谁能简化一下... [打印本页]

作者: lateol    时间: 2013-5-27 22:09     标题: 怎么提高文本替换效率与简洁性,这有短代码谁能简化一下...

本帖最后由 lateol 于 2013-5-28 18:12 编辑
  1. pushd "==NC==\"
  2. ::===D===
  3. fr *.TXT *.NC -r:"***:( DIA. = 21.000   R = 0.8000 )" -t:"Cutter Name: D21R0.8"
  4. fr *.TXT *.NC -r:"***:( DIA. = 20.000   R = 1.0000 )" -t:"Cutter Name: D20R1"
  5. fr *.TXT *.NC -r:"***:( DIA. = 20.000   R = 000000 )" -t:"Cutter Name: D20"
  6. fr *.TXT *.NC -r:"***:( DIA. = 17.000   R = 0.8000 )" -t:"Cutter Name: D17R0.8"
  7. fr *.TXT *.NC -r:"***:( DIA. = 12.000   R = 0.5000 )" -t:"Cutter Name: D12R0.5"
  8. fr *.TXT *.NC -r:"***:( DIA. = 12.000   R = 0.2000 )" -t:"Cutter Name: D12R0.2"
  9. fr *.TXT *.NC -r:"***:( DIA. = 12.000   R = 000000 )" -t:"Cutter Name: D12"
  10. fr *.TXT *.NC -r:"***:( DIA. = 10.000   R = 0.5000 )" -t:"Cutter Name: D10R0.5"
  11. fr *.TXT *.NC -r:"***:( DIA. = 10.000   R = 0.2000 )" -t:"Cutter Name: D10R0.2"
  12. fr *.TXT *.NC -r:"***:( DIA. = 10.000   R = 000000 )" -t:"Cutter Name: D10"
  13. fr *.TXT *.NC -r:"***:( DIA. = 8.000   R = 0.5000 )" -t:"Cutter Name: D8R0.5"
  14. fr *.TXT *.NC -r:"***:( DIA. = 8.000   R = 0.2000 )" -t:"Cutter Name: D8R0.2"
  15. fr *.TXT *.NC -r:"***:( DIA. = 8.000   R = 000000 )" -t:"Cutter Name: D8"
  16. fr *.TXT *.NC -r:"***:( DIA. = 6.000   R = 0.5000 )" -t:"Cutter Name: D6R0.5"
  17. fr *.TXT *.NC -r:"***:( DIA. = 6.000   R = 0.2000 )" -t:"Cutter Name: D6R0.2"
  18. fr *.TXT *.NC -r:"***:( DIA. = 6.000   R = 000000 )" -t:"Cutter Name: D6"
  19. fr *.TXT *.NC -r:"***:( DIA. = 4.000   R = 0.5000 )" -t:"Cutter Name: D4R0.5"
  20. fr *.TXT *.NC -r:"***:( DIA. = 4.000   R = 0.2000 )" -t:"Cutter Name: D4R0.2"
  21. fr *.TXT *.NC -r:"***:( DIA. = 4.000   R = 000000 )" -t:"Cutter Name: D4"
  22. fr *.TXT *.NC -r:"***:( DIA. = 3.000   R = 0.2000 )" -t:"Cutter Name: D3R0.2"
  23. fr *.TXT *.NC -r:"***:( DIA. = 3.000   R = 000000 )" -t:"Cutter Name: D3"
  24. fr *.TXT *.NC -r:"***:( DIA. = 2.000   R = 000000 )" -t:"Cutter Name: D2"
  25. fr *.TXT *.NC -r:"***:( DIA. = 1.000   R = 000000 )" -t:"Cutter Name: D1"
  26. ::===R===
  27. fr *.TXT *.NC -r:"***:( DIA. = 12.000   R = 6.0000 )" -t:"Cutter Name: D12R6"
  28. fr *.TXT *.NC -r:"***:( DIA. = 10.000   R = 5.0000 )" -t:"Cutter Name: D10R5"
  29. fr *.TXT *.NC -r:"***:( DIA. = 8.000   R = 4.0000 )" -t:"Cutter Name: D8R4"
  30. fr *.TXT *.NC -r:"***:( DIA. = 6.000   R = 3.0000 )" -t:"Cutter Name: D6R3"
  31. fr *.TXT *.NC -r:"***:( DIA. = 4.000   R = 2.0000 )" -t:"Cutter Name: D4R2"
  32. fr *.TXT *.NC -r:"***:( DIA. = 3.000   R = 1.5000 )" -t:"Cutter Name: D3R1.5"
  33. fr *.TXT *.NC -r:"***:( DIA. = 2.000   R = 1.0000 )" -t:"Cutter Name: D2R1"
  34. fr *.TXT *.NC -r:"***:( DIA. = 1.5000   R = 0.7500 )" -t:"Cutter Name: D1.5R0.75"
  35. fr *.TXT *.NC -r:"***:( DIA. = 1.000   R = 0.5000 )" -t:"Cutter Name: D1R0.5"
  36. fr *.TXT *.NC -r:"***:( DIA. = 0.6000   R = 0.3000 )" -t:"Cutter Name: D0.6R0.3"
  37. fr *.TXT *.NC -r:"***:( DIA. = 0.5000   R = 0.2500 )" -t:"Cutter Name: D0.5R0.25"
  38. ::====================TZE================================
  39. fr *.TXT *.NC -r:"***:Dia:21.00  Tip Radius:.8" -t:"Cutter Name: D21R0.8"
  40. fr *.TXT *.NC -r:"***:Dia:20.00  Tip Radius:1" -t:"Cutter Name: D20R1"
  41. fr *.TXT *.NC -r:"***:Dia:17.00  Tip Radius:.8" -t:"Cutter Name: D17R0.8"
  42. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:6" -t:"Cutter Name: D12R6"
  43. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:.5" -t:"Cutter Name: D12R0.5"
  44. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:.2" -t:"Cutter Name: D12R0.2"
  45. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:5" -t:"Cutter Name: D10R5"
  46. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:.5" -t:"Cutter Name: D10R0.5"
  47. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:.2" -t:"Cutter Name: D10R0.2"
  48. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:4" -t:"Cutter Name: D8R4"
  49. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:.5" -t:"Cutter Name: D8R0.5"
  50. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:0.2" -t:"Cutter Name: D8R0.2"
  51. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:3" -t:"Cutter Name: D6R3"
  52. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:.5" -t:"Cutter Name: D6R0.5"
  53. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:.2" -t:"Cutter Name: D6R0.2"
  54. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:2" -t:"Cutter Name: D4R2"
  55. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:.5" -t:"Cutter Name: D4R0.5"
  56. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:.2" -t:%2
复制代码
不好意思 代码没贴完整
  1. ::====================TZE================================
  2. fr *.TXT *.NC -r:"***:Dia:21.00  Tip Radius:.8" -t:"Cutter Name: D21R0.8"
  3. fr *.TXT *.NC -r:"***:Dia:20.00  Tip Radius:1" -t:"Cutter Name: D20R1"
  4. fr *.TXT *.NC -r:"***:Dia:17.00  Tip Radius:.8" -t:"Cutter Name: D17R0.8"
  5. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:6" -t:"Cutter Name: D12R6"
  6. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:.5" -t:"Cutter Name: D12R0.5"
  7. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:.2" -t:"Cutter Name: D12R0.2"
  8. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:5" -t:"Cutter Name: D10R5"
  9. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:.5" -t:"Cutter Name: D10R0.5"
  10. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:.2" -t:"Cutter Name: D10R0.2"
  11. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:4" -t:"Cutter Name: D8R4"
  12. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:.5" -t:"Cutter Name: D8R0.5"
  13. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:0.2" -t:"Cutter Name: D8R0.2"
  14. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:3" -t:"Cutter Name: D6R3"
  15. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:.5" -t:"Cutter Name: D6R0.5"
  16. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:.2" -t:"Cutter Name: D6R0.2"
  17. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:2" -t:"Cutter Name: D4R2"
  18. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:.5" -t:"Cutter Name: D4R0.5"
  19. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:.2" -t:"Cutter Name: D4R0.2"
  20. fr *.TXT *.NC -r:"***:Dia:3.00  Tip Radius:1.5" -t:"Cutter Name: D3R1.5"
  21. fr *.TXT *.NC -r:"***:Dia:3.00  Tip Radius:.2" -t:"Cutter Name: D3R0.2"
  22. fr *.TXT *.NC -r:"***:Dia:2.00  Tip Radius:1" -t:"Cutter Name: D2R1"
  23. fr *.TXT *.NC -r:"***:Dia:1.00  Tip Radius:.5" -t:"Cutter Name: D1R0.5"
  24. fr *.TXT *.NC -r:"***:Dia:1.500  Tip Radius:.75" -t:"Cutter Name: D1.5R0.75"
  25. fr *.TXT *.NC -r:"***:Dia:0.600  Tip Radius:.3" -t:"Cutter Name: D0.6R0.3"
  26. fr *.TXT *.NC -r:"***:Dia:0.500  Tip Radius:.25" -t:"Cutter Name: D0.5R0.25"
  27. fr *.TXT *.NC -r:"***:Dia:20.00  Tip Radius:0" -t:"Cutter Name: D20"
  28. fr *.TXT *.NC -r:"***:Dia:12.00  Tip Radius:0" -t:"Cutter Name: D12"
  29. fr *.TXT *.NC -r:"***:Dia:10.00  Tip Radius:0" -t:"Cutter Name: D10"
  30. fr *.TXT *.NC -r:"***:Dia:8.00  Tip Radius:0" -t:"Cutter Name: D8"
  31. fr *.TXT *.NC -r:"***:Dia:6.00  Tip Radius:0" -t:"Cutter Name: D6"
  32. fr *.TXT *.NC -r:"***:Dia:4.00  Tip Radius:0" -t:"Cutter Name: D4"
  33. fr *.TXT *.NC -r:"***:Dia:3.00  Tip Radius:0" -t:"Cutter Name: D3"
  34. fr *.TXT *.NC -r:"***:Dia:2.00  Tip Radius:0" -t:"Cutter Name: D2"
  35. fr *.TXT *.NC -r:"***:Dia:1.00  Tip Radius:0" -t:"Cutter Name: D1"
  36. ::====================TZE2================================
  37. fr *.TXT *.NC -r:"***:TOOL NAME =" -t:"Cutter Name:"
  38. for %%a in (D21R0.8 D20R1 D17R0.8 D12R6 D12R0.5 D12R0.2 D10R5 D10R0.5 D10R0.2 D8R4 D8R0.5 D8R0.2 D6R3 D6R0.5 D6R0.2 D4R2 D4R0.5 D4R0.2 D3R1.5 D3R0.2 D2R1 D1R0.5 D0.6R0.3 D0.5R0.25 D20 D12 D10 D8 D6 D4 D3 D2 D1 R6 R5 R4 R3 R2 R1 R0.75 R0.5) do (
  39.   for /f "delims=:" %%b in ('findstr /ic:"Cutter Name: %%a" *.NC') do (
  40.     md "%%a" 2>nul
  41.     move "%%b" "%%a\"
  42.   )
  43. )
  44. echo    ============【============处理完毕...============】============
  45. GOTO QQ
复制代码

作者: sxw    时间: 2013-5-28 07:40

这是想干啥
作者: Batcher    时间: 2013-5-28 08:38

请给出原始文本和希望得到的文本,详细描述替换规律。
作者: tmplinshi    时间: 2013-5-28 10:43

bwfr - 替换文本中的多个字符串
http://bathome.net/thread-12910-1-1.html
作者: lateol    时间: 2013-5-28 18:19

回复 3# Batcher


    已经把文本上到百度云,
http://pan.baidu.com/share/link?shareid=684456&uk=774360720
文件比较多 顺便上传了几个。

以下分别为五种显示类型在在每个文本头显示的信息,我这里叫做刀具信息。

1.
(  Dia:10.00  Tip Radius:0  Tool length:50.00 )


2.
( T - 1   L - 40   DIA. = 8.000   1_2_2 )
( DIA. = 8.000   R = 000000 )

( T - 1   L - 20   DIA. = 4.000   1_2_2_1 )
( DIA. = 4.000   R = 000000 )

( T - 1   L - 20   DIA. = 4.000   43 )
( DIA. = 4.000   R = 2.0000 )

( T - 1   L - 40   DIA. = 8.000   38_1_1_1 )
( DIA. = 8.000   R = 000000 )

3.
(Cutter Name: D12)

(Cutter Name: D6R3)

(Cutter Name: D4R2)

(Cutter Name: D4)

(Cutter Name: D1R0.5)

4.
( TOOL NAME = R3 , D = 6.0 , R = 3. )
( PART OFST = 0.0 , PART TOL. = 0.01 )
( DOWN STEP = 0.3 , SIDE STEP = 4.92 )

( TOOL NAME = D12 , D = 12.0 , R = 0.0 )
( PART OFST = 0.2 , PART TOL. = 0.01 )
( DOWN STEP = 0.8 , SIDE STEP = 6. )

( TOOL NAME = R1.5 , D = 3.0 , R = 1.5 )
( PART OFST = -0.03 , PART TOL. = 0.01 )
( DOWN STEP = 0.3 , SIDE STEP = 2.88 )

( TOOL NAME = D4 , D = 4.0 , R = 0.0 )
( PART OFST = 0.25 , PART TOL. = 0.01 )
( DOWN STEP = 0.12 , SIDE STEP = 2. )

( TOOL NAME = D4R0.5 , D = 4.0 , R = 0.5 )
( PART OFST = -0.07 , PART TOL. = 0.005 )
( DOWN STEP = 0.12 , SIDE STEP = 2. )
5.
(Tool Name: D12)

(Tool Diameter=12.00 )

(Tool Radius=0.00 )

刀具信息种类:
D21R0.8 D20R1 D17R0.8 D12R6 D12R0.5 D12R0.2 D10R5 D10R0.5 D10R0.2 D8R4 D8R0.5 D8R0.2 D6R3 D6R0.5 D6R0.2 D4R2 D4R0.5 D4R0.2 D3R1.5 D3R0.2 D2R1 D1R0.5 D0.6R0.3 D0.5R0.25 D20 D12 D10 D8 D6 D4 D3 D2 D1 R6 R5 R4 R3 R2 R1 R0.75 R0.5
流程:用
  1. for %%a in (D21R0.8 D20R1 D17R0.8 D12R6 D12R0.5 D12R0.2 D10R5 D10R0.5 D10R0.2 D8R4 D8R0.5 D8R0.2 D6R3 D6R0.5 D6R0.2 D4R2 D4R0.5 D4R0.2 D3R1.5 D3R0.2 D2R1 D1R0.5 D0.6R0.3 D0.5R0.25 D20 D12 D10 D8 D6 D4 D3 D2 D1 R6 R5 R4 R3 R2 R1 R0.75 R0.5) do (
  2.   for /f "delims=:" %%b in ('findstr /ic:"Cutter Name: %%a" *.NC') do (
  3.     md "%%a" 2>nul
  4.     move "%%b" "%%a\"
  5.   )
  6. )
复制代码
查找文本内容信息 找到后重名个文件夹 名字就是查找到的信息 如 (Cutter Name: D6R3) 则新建D6R3文件夹 并移动进去。以此类推。
如果 所有的文本内容的信息都显示一样 如(Cutter Name: D6R3) 这个就好办多了。可是有五种方式显示,所以部分识别不到,处理不成功。所以出现了以上方法,先替换 再识别。
作者: terse    时间: 2013-5-28 19:06

回复 5# lateol
是否想得到信息呢 还是其他
  1. @echo off
  2. findstr /ibc:"( TOOL NAME = " /c:"( PART OFST = " /c:"( DOWN STEP = " *.nc
  3. pause
复制代码

作者: lateol    时间: 2013-5-28 20:12

回复 6# terse

是的 比如说 此文本内包含(Cutter Name: D4R2) 则创建 D4R2 文件夹 并把它移入内。就达到按信息分类的意思。
作者: lateol    时间: 2013-5-28 20:23

本帖最后由 lateol 于 2013-5-28 20:25 编辑

回复 6# terse


   附件的文本 显示的信息类型是  
( TOOL NAME = R1 , D = 2.0 , R = 1. )
这个刀具信息为 D2R1
有点跟(Cutter Name: D2R1)
一样 所以我就用FR替换
( TOOL NAME = R1 , D = 2.0 , R = 1. ) 替换成(Cutter Name: D2R31)
这样就 可以分类。主要问题还是 信息显示不太统一 有五种,所以无法一举处理。
除非可以再写个 其他形式的BAT
谢谢你的帮忙。
作者: terse    时间: 2013-5-28 23:22

回复 8# lateol
仅针对你上面样本测试处理的
即你所诉第四种情况吧 不知是否你要的结果
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=1* delims=:" %%i in ('findstr /ic:"TOOL NAME = " *.nc') do (
  3.     if "!%%i!" == "" (
  4.     for /f "tokens=4,6 delims==,)" %%a in ("%%j") do (
  5.         set D=%%a&set R=%%b
  6.         for %%c in (D R) do for %%d in (!%%c!) do (
  7.             set %%c=%%d
  8.             for /l %%e in (1 1 6) do (
  9.                if "!%%c:~-1!" == "0" set "%%c=!%%c:~,-1!"
  10.             )
  11.             if "!%%c!" neq "0." (
  12.                 if "!%%c:~-1!" == "." set %%c=!%%c:~,-1!
  13.                 set %%c=%%c!%%c!
  14.             )else set "%%c="
  15.         )
  16.         echo !D!!R!
  17.     )
  18.   )
  19.         set "%%i=i"
  20. )
  21. pause
复制代码

作者: lateol    时间: 2013-5-29 00:11

回复 9# terse


    对! 是这样。不过还有四个信息类型可以整合么。
随便在 文本内容里换个信息类型在测试、看看
只要 能正确识别五种类型信息 并进行分类 基本上就OK了 想都有点难度..- -!

代码 我在19行 加入md "!D!!R!" 2>nul
    move "%%i" "!D!!R!\"
就这样分类 就达到效果了。
谢谢 你的代码 学习了、
作者: terse    时间: 2013-5-29 00:45

因你的样本里不含有其他四个信息类型
所以不一点测试完全正确
还有就是  每个文本含四个信息的其中一个信息吗  不会重复吧
作者: lateol    时间: 2013-5-29 07:40

回复 11# terse


    没有重复的
作者: terse    时间: 2013-5-29 10:07

回复 12# lateol
没完全测试 不知信息是否完整
"(Tool Name:" "(Cutter Name:" 这两信息点 多余否?
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=1,2* delims=:" %%i in ('findstr /ic:"TOOL NAME = " /c:"(Cutter Name:" /c:"( DIA. = " /c:"(  Dia:" /c:"(Tool Name:" *.nc') do (
  3.     if "!%%i!" == "" (   
  4.        set "str="
  5.        if "%%k" == "" (
  6.           for /f "tokens=2-6 delims==,)" %%a in ("%%j") do (
  7.               if "%%c" =="" (
  8.                  set D=%%a&set "R=%%b"
  9.               ) else set D=%%c&set "R=%%e"
  10.           )
  11.        ) else for /f "tokens=1-6 delims=:) " %%a in ("%%k") do (
  12.                   if "%%d" neq "" (
  13.                       set D=%%a&set "R=%%d"
  14.                   ) else set "str=%%a"
  15.               )
  16.              if "!str!" == "" (
  17.                 for %%f in (D R) do for /f %%h in ("!%%f!") do (
  18.                     set "%%f=%%h"
  19.                     for /l %%e in (1 1 6) do (
  20.                         if "!%%f:~-1!" == "0" set "%%f=!%%f:~,-1!"
  21.                    )
  22.                         if "!%%f!" neq "0." (
  23.                            if "!%%f:~-1!" == "." set "%%f=!%%f:~,-1!"
  24.                            set "%%f=%%f!%%f!"
  25.                         ) else set "%%f="
  26.                 )
  27.                           set "str=!D!!R!"
  28.             )
  29.                           echo "!str!"
  30.                           REM md "!str!"
  31.                           REM move "%%i" "!str!"
  32.                           REM set "%%i=i"
  33.     )
  34. )
  35. pause
复制代码

作者: lateol    时间: 2013-5-29 23:44

回复 13# terse


    很好 测试完美 唯一的就是 处理到 ( DIA. = 4.000   R = 000000 )
这样是 因为 R是0  所以得出D4R
不过自己用变量来截取了。其他都OK
请问这个 set "str="  是什么意思?
谢谢!
作者: terse    时间: 2013-5-29 23:55

回复 14# lateol
24行 替为
  1. if "!%%f!" neq "" set "%%f=%%f!%%f!"
复制代码
SET "STR="  清空变量




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