Board logo

标题: [文本处理] 求助如何使用BAT脚本批量修改文本里的内容并删除重复行? [打印本页]

作者: a545910540    时间: 2021-10-27 14:27     标题: 求助如何使用BAT脚本批量修改文本里的内容并删除重复行?

大佬们求助,这是要修改的内容,想把2021前的1和2统一改成一样的,然后在删除文本里重复的内容
例如:
N2310000000000114262120211015160049-2
N2310000000000114262220211015160049-2
N2310000000000114502120211015160027-0
N2310000000000114502220211015160027-0
N2310000000000115452120211015160059-1
N2310000000000115452220211015160059-1
N2310000000000115752120211015160022-1
N2310000000000115752220211015160022-1
N2310000000000116822120211015160028-2
N2310000000000116822220211015160028-2
N2310000000000116842120211015160021-2
N2310000000000116842220211015160021-2
N2310000000000117262120211015160007-1
N2310000000000117262220211015160007-1
N2310000000000114262120211015160049-2
N2310000000000114262220211015160049-2
N2310000000000114502120211015160027-0
N2310000000000114502220211015160027-0
N2310000000000115452120211015160059-1
N2310000000000115452220211015160059-1
N2310000000000115752120211015160022-1
N2310000000000115752220211015160022-1
N2310000000000116822120211015160028-2
N2310000000000116822220211015160028-2
N2310000000000116842120211015160021-2
N2310000000000116842220211015160021-2
N2310000000000117262120211015160007-1
N2310000000000117262220211015160007-1
作者: idwma    时间: 2021-10-27 15:32

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f "delims=" %%i in (1.txt) do (
  4.     set a=%%i
  5.     set a=!a:~0,19!一样的!a:~21!
  6.     if not defined !a! (
  7.         set !a!=1
  8.         echo;!a!
  9.     )
  10. ))>2.txt
  11. pause
复制代码

作者: a545910540    时间: 2021-10-27 15:38

本帖最后由 a545910540 于 2021-10-27 15:43 编辑

回复 2# idwma


    大佬,只要重复的连原信息都删除,结果应该一条记录都没有,这个该怎么弄呢
N231000000000011426一样的20211015160049
N231000000000011450一样的20211015160027
N231000000000011545一样的20211015160059
N231000000000011575一样的20211015160022
N231000000000011682一样的20211015160028
N231000000000011684一样的20211015160021
N231000000000011726一样的20211015160007
作者: idwma    时间: 2021-10-27 16:31

回复 3# a545910540
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=-" %%i in (1.txt) do (
  4.     set a=%%i
  5.     set a=!a:~0,19!一样的!a:~21!
  6.     if not defined !a! (
  7.         set /a #!a!+=1
  8.     )
  9. )
  10. for /f "tokens=1,2 delims=#=" %%i in ('set #') do if %%j==1 echo %%i
  11. pause
复制代码

作者: a545910540    时间: 2021-10-28 08:29

本帖最后由 a545910540 于 2021-10-28 08:53 编辑

回复 4# idwma


    谢谢,大佬,敢问你从哪学的bat脚本,我也想系统的学一下

修改之前:N2310000000000114262120211015160049-2-黑C5Y959.jpg
              N2310000000000114262220211015160049-2-黑C5Y959.jpg
修改之后:N231000000000011426一样的20211015160049-2-黑C5Y959.jpg
              N231000000000011426一样的20211015160049-2-黑C5Y959.jpg
改成这种形式的话要截取多少位呢,我现在怎么改 “-”后面的内容都带不出来
作者: a545910540    时间: 2021-10-28 14:08

回复 6# qixiaobin0715


    原数据:
   N2310000000000113702120211020082127-2-黑C84L88
   N2310000000000114262120211015160049-2-黑C5Y959
   N2310000000000114262220211015160049-2-黑C5Y959
   执行上述代码后
   新数据:
   N231000000000011370一样的20211020082127
   如何截取实现字符“-”后面的数据也显示
作者: a545910540    时间: 2021-10-28 14:27

回复 8# qixiaobin0715

N2310000000000113702120211020082127-2-黑C84L88.jpg
N2310000000000114262120211015160049-2-黑C5Y959.jpg
N2310000000000114262220211015160049-2-黑C5Y959.jpg
N2310000000000114502120211015160027-0-黑C2295警.jpg
N2310000000000114502220211015160027-0-黑C2295警.jpg
N2310000000000115452120211015160059-1-黑D18707.jpg
N2310000000000115452220211015160059-1-黑D18707.jpg
N2310000000000115752120211015160022-1-黑C22562.jpg
N2310000000000115752220211015160022-1-黑C22562.jpg
N2310000000000116822120211015160028-2-黑CU6866.jpg
N2310000000000116822220211015160028-2-黑CU6866.jpg
N2310000000000116842120211015160021-2-宁M53087.jpg
N2310000000000116842220211015160021-2-宁M53087.jpg
N2310000000000117262120211015160007-1-宁G74584.jpg
N2310000000000117262220211015160007-1-宁G74584.jpg

@echo off
setlocal enabledelayedexpansion
set path=2021
dir /B !path!>0000.txt
for /f "delims=-" %%i in (0000.txt) do (
    set a=%%i
    set a=!a:~0,19!一样的!a:~21!
    if not defined !a! (
        set /a #!a!+=1
    )
)
for /f "tokens=1,2 delims=#=" %%i in ('set #') do if %%j==1 echo %%i>1111.txt
pause

执行后

N231000000000011370一样的20211020082127
"-"后面内容没有输出出来
作者: a545910540    时间: 2021-10-28 14:44

回复 10# qixiaobin0715


    去掉 - 的话运行不出结果了
作者: idwma    时间: 2021-10-28 14:48

回复 5# a545910540


    没有系统的学过呀,在这里多回答问题不会的就搜以前的帖子灵活运用.
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=1* delims=-" %%i in (1.txt) do (
  4.     set a=%%i
  5.     set a=!a:~0,19!一样的!a:~21!
  6.     set /a !a!+=1
  7.     call;set #!a!-%%j=%%!a!%%
  8. )
  9. for /f "tokens=1,2 delims=#=" %%i in ('set #') do if %%j==1 echo %%i
  10. pause
复制代码

作者: a545910540    时间: 2021-10-28 14:56

回复 11# idwma


   大佬,太强了,以后我也得多混混论坛
作者: Batcher    时间: 2021-10-28 15:07

回复 9# a545910540


path是系统默认环境变量,如果自定义变量跟它重名的话会导致很多问题,以后写其他脚本的时候请留意。

推荐阅读:批处理新手入门导读
http://bbs.bathome.net/thread-75-1-1.html
作者: a545910540    时间: 2021-11-4 09:08

本帖最后由 a545910540 于 2021-11-4 09:35 编辑

回复 9# idwma




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