[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]批处理自动删除多余的答案,只保留一个答案。

1、目的:希望拖入文本,自动删除多余的答案,只保留一个答案。

2、材料:
原文本:
Author: Serg Belyaev

###########
##  .##@  #
# $ ...$  #
#  $#*##  #
#   $.  $ #
####   ####
###########
; Level 1
Title: level 1
Author: Serg Belyaev
Solution (pushes 38, moves 92, inlines 20, changes 14, steps 16 PMIS): rdLLLLullDRRRRRurDlllllllddrrRdrruLUdLLUUrRRRurrdLLLrrrddLUruLLrddLLdlUlluullddRRRllUluRRluR
Solution (pushes 42, moves 126, inlines 21, changes 12, steps 20 C): rdLLLLullDRRRRRurDlllllllddrrUdlluururrdLddlluRdrUUddRdrruLLLrrUdlluurRRRurrdLLrrddLLLdlUUdllulluRRluRdllddRRRdrUrrrruulDrdLLL

##########
##     ###
#  #.#$ ##
#  *..   #
## #*#$  #
##$@   ###
##     ###
##########
; Level 2
Title: level 2
Author: Serg Belyaev
Solution (pushes 24, moves 89, inlines 17, changes 14, steps 15 PMIS): dlUUURRRRllDuuurrDrDLLruullllDldRddrRuUdddrruUdLdlUlluuuurrrrdrdrdLuLLruullllddddrrrrUruL
Solution (pushes 26, moves 127, inlines 18, changes 11, steps 15 C): dlUUURRDullddrRuuuullDldRRlddrruUdddrruLdlUlluurrRRlllluurrrrDrDLLLrruullllddddrrrrUdlllluuuurrrrdrdLLrrrdLuluullllddddrrrrUruL

###########
###@ .  ###
#   #.#$  #
#   *.*   #
###$#.#$###
###   $   #
####  #   #
###########
; Level 3
Title: level 3
Author: Serg Belyaev
Solution (pushes 50, moves 154, inlines 27, changes 21, steps 23 PMIS): rrddddllUUddrrRRdrruLLLLuuRRDulluurrDDllLLddRdrUUUrrurrdLLLLddrRUdlluurRuullllDDRRUdddrruuLrddlluUllulldRRRRddllUdrrrrdrruLLLLuulLulldRRddRdrUrruuLrurrdLL
Solution (pushes 52, moves 218, inlines 28, changes 17, steps 28 C): rrddLLrrRRllddllUUddrrRRdrruLLLLuurrDullllddRdrUUdlluurRddrRdrruLLLLuullddRdrUlluurruurrDDDuuullddllddrrrRuuuullllDDuurrrrddddllUUUdddrruuLrddlluUllulldRRRRddllUdrruulLddrrrrdrruLLLLuullddRdrUlluuulldRRddrrrruuLrurrdLL


希望生成的文本:
Author: Serg Belyaev

###########
##  .##@  #
# $ ...$  #
#  $#*##  #
#   $.  $ #
####   ####
###########
; Level 1
Title: level 1
Author: Serg Belyaev
Solution (pushes 38, moves 92, inlines 20, changes 14, steps 16 PMIS): rdLLLLullDRRRRRurDlllllllddrrRdrruLUdLLUUrRRRurrdLLLrrrddLUruLLrddLLdlUlluullddRRRllUluRRluR

##########
##     ###
#  #.#$ ##
#  *..   #
## #*#$  #
##$@   ###
##     ###
##########
; Level 2
Title: level 2
Author: Serg Belyaev
Solution (pushes 24, moves 89, inlines 17, changes 14, steps 15 PMIS): dlUUURRRRllDuuurrDrDLLruullllDldRddrRuUdddrruUdLdlUlluuuurrrrdrdrdLuLLruullllddddrrrrUruL

###########
###@ .  ###
#   #.#$  #
#   *.*   #
###$#.#$###
###   $   #
####  #   #
###########
; Level 3
Title: level 3
Author: Serg Belyaev
Solution (pushes 50, moves 154, inlines 27, changes 21, steps 23 PMIS): rrddddllUUddrrRRdrruLLLLuuRRDulluurrDDllLLddRdrUUUrrurrdLLLLddrRUdlluurRuullllDDRRUdddrruuLrddlluUllulldRRRRddllUdrrrrdrruLLLLuulLulldRRddRdrUrruuLrurrdLL


3、我的代码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%i in (%~nx1)do (
  4. set m=%%i
  5. if defined z (echo.)else echo !m!
  6. if "!m:~0,8!" == "Solution" (set z=%%i)else set z=
  7. )>>%~n1-2.txt
  8. endlocal
  9. pause
  10. goto:eof
复制代码
运行结果:

Author: Serg Belyaev
###########
##  .##@  #
# $ ...$  #
#  $#*##  #
#   $.  $ #
####   ####
###########
Title: level 1
Author: Serg Belyaev
Solution (pushes 38, moves 92, inlines 20, changes 14, steps 16 PMIS): rdLLLLullDRRRRRurDlllllllddrrRdrruLUdLLUUrRRRurrdLLLrrrddLUruLLrddLLdlUlluullddRRRllUluRRluR


##     ###
#  #.#$ ##
#  *..   #
## #*#$  #
##$@   ###
##     ###
##########
Title: level 2
Author: Serg Belyaev
Solution (pushes 24, moves 89, inlines 17, changes 14, steps 15 PMIS): dlUUURRRRllDuuurrDrDLLruullllDldRddrRuUdddrruUdLdlUlluuuurrrrdrdrdLuLLruullllddddrrrrUruL


###@ .  ###
#   #.#$  #
#   *.*   #
###$#.#$###
###   $   #
####  #   #
###########
Title: level 3
Author: Serg Belyaev
Solution (pushes 50, moves 154, inlines 27, changes 21, steps 23 PMIS): rrddddllUUddrrRRdrruLLLLuuRRDulluurrDDllLLddRdrUUUrrurrdLLLLddrRUdlluurRuullllDDRRUdddrruuLrddlluUllulldRRRRddllUdrrrrdrruLLLLuulLulldRRddRdrUrruuLrurrdLL

每组第一行怎样都丢掉了?错在什么地方,请高手指教。谢谢!
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 7# pan528


    我在win10测试能够运行,你用的什么系统? 与2楼也就只有绝对路径与相对路径之间的区别
把 "%~f1" 改成 "%~nx1"
把 "%~dpn1-2%~x1" 改成 "%~n1-2.txt"
试下

TOP

回复 6# WHY

谢谢回帖!

但,这两个代码都没有通过测试。

TOP

回复 4# pan528


    这样好了:
  1. @echo off & setlocal enabledelayedexpansion
  2. (for /f "delims="eol^= %%i in ('type "%~f1" ^& echo;/')do (
  3.     set "s1=%%i"
  4.     if defined z (echo;) else echo;!s2!
  5.     if "!s1:~0,8!" == "Solution" (if "!s2:~0,8!" == "Solution" set z=1) else (set z=)
  6.     set "s2=%%i"
  7. ))>"%~dpn1-2%~x1"
复制代码
  1. @if (0)==(0) echo off
  2. type "%~f1" | cscript //nologo //e:jscript "%~f0" > "%~dpn1-2%~x1"
  3. pause & exit
  4. @end
  5. var s = WSH.StdIn.ReadAll() + '\r\n'
  6. WSH.StdOut.Write(s.replace(/(^Solution)(.*\n)(\1.*\n)+/gm, '$1$2'))
复制代码

TOP

回复 3# WHY

这个代码OK!谢谢!

TOP

RE: 希望自动删除多余的答案,只保留一个答案。

本帖最后由 pan528 于 2017-2-9 12:52 编辑

回复 2# WHY

谢谢回贴!
但问题如故。

“:”行丢掉不重要。

是图形的第一行丢掉了!

如:

######
#    #
#$##.#
## ##.##
#  @$. #
# $    #
###  ###
  ####  

运行后,变成:

#    #
#$##.#
## ##.##
#  @$. #
# $    #
###  ###
  ####  

你看错在哪里?

TOP

  1. @echo off & setlocal enabledelayedexpansion
  2. (for /f "delims=" %%i in ('findstr /n .* "%~nx1"')do (
  3.     set "m=%%i" & set "m=!m:*:=!"
  4.     if defined z (echo;)else echo;!m!
  5.     if "!m:~0,8!" == "Solution" (set z=1) else (set z=)
  6. ))>"%~n1-2.txt"
复制代码
1

评分人数

TOP

for /f 默认会忽略以分号开头的行
可以这样:
  1. @echo off & setlocal enabledelayedexpansion
  2. (for /f "delims="eol^= %%i in ('type "%~nx1"')do (
  3.     set "m=%%i"
  4.     if defined z (echo;)else echo;!m!
  5.     if "!m:~0,8!" == "Solution" (set z=1) else (set z=)
  6. ))>"%~n1-2.txt"
  7. pause
复制代码

TOP

返回列表