返回列表 发帖

[文本处理] 请问去重复行的批处理如何修改为批量处理?

@echo off
setlocal
(for /f "delims=" %%i in (a.txt) do (
    if not defined %%i (
        echo,%%i
        set %%i=1
    )
))>b.txt
endlocal
pauseCOPY
改为批量执行会错误, 结果不对, 请问如何修改为批量处理
@echo off
setlocal
(for /f "delims=" %%i in (%%i.txt) do (
    if not defined %%i (
        echo,%%i
        set %%i=1
    )
))>%%i.txt
endlocal
pauseCOPY

%%i.txt  是什么
@echo off
setlocal
for /f "delims=" %%a in ('dir /b *.txt') do (
(for /f "delims=" %%i in (%%a) do (
    if not defined %%i (
        echo,%%i
        set %%i=1
    )
    set  %%i=
)))>%%a.txt
endlocal
pauseCOPY

TOP

本帖最后由 娜美 于 2023-9-18 17:25 编辑

回复 2# pd1
@echo off
setlocal
for /f "delims=" %%a in ('dir /b *.txt') do (
(for /f "delims=" %%i in (%%a) do (
    if not defined %%i (
        echo,%%i
        set %%i=1
    )
    set  %%i=
)))>%%a.txt
endlocal
pauseCOPY
主题代码是去重复行的  执行后, 输出了整个原文本, 似乎失效,  不知道啥原因

1.txt
C
B
A
C

2.txt
A
B
C
A

TOP

@echo off
powershell -c "dir *.txt|%{gc $_|Select-Object -Unique|sc $_}"
pauseCOPY

TOP

我不知道那个是去重的,所以就随便改了应该是我改的有问题吧
批处理水平不行,还得再学习

TOP

Python 保存xxx.py  要先安装Python
# 获取输入文件名
input_file = input("请输入需要去除重复行的文本文件名: ")
# 确保输入文件存在
try:
    with open(input_file, 'r', encoding='utf-8') as file:
        lines = file.readlines()
except FileNotFoundError:
    print(f"文件 '{input_file}' 不存在,请确保文件名正确。")
    exit(1)
# 创建输出文件名
output_file = input_file.replace('.', '-BuCF.')
# 去除重复行并保存到输出文件,保持原始顺序
unique_lines = []
seen = set()
for line in lines:
    if line not in seen:
        unique_lines.append(line)
        seen.add(line)
with open(output_file, 'w', encoding='utf-8') as file:
    file.writelines(unique_lines)
print(f"已去除重复行并保存到 '{output_file}' 文件中。")COPY

TOP

批量版本的
import os
# 创建 output 子目录
if not os.path.exists('output'):
    os.mkdir('output')
# 获取当前目录下的所有 *.txt 文件
txt_files = [filename for filename in os.listdir() if filename.endswith('.txt')]
for input_file in txt_files:
    # 确保输入文件存在
    try:
        with open(input_file, 'r', encoding='utf-8') as file:
            lines = file.readlines()
    except FileNotFoundError:
        print(f"文件 '{input_file}' 不存在,请确保文件名正确。")
        continue
    # 创建输出文件名
    output_file = os.path.join('output', input_file.replace('.', '-BuCF.'))
    # 去除重复行并保存到输出文件,保持原始顺序
    unique_lines = []
    seen = set()
    for line in lines:
        if line not in seen:
            unique_lines.append(line)
            seen.add(line)
    with open(output_file, 'w', encoding='utf-8') as file:
        file.writelines(unique_lines)
    print(f"已去除重复行并保存到 'output/{output_file}' 文件中。")COPY

TOP

哥哥们怎么搞的变复杂了

只想将主题代码改为批量执行后输出对应文件名就行
2楼哥哥理解正确, 但是执行后没有发挥作用
用bat只因为要添加到bat脚本中,

TOP

本帖最后由 Five66 于 2023-9-18 20:41 编辑

已编辑,看10~12楼

TOP

回复 9# Five66


    set %%i=1这里可能会有问题,可能会有行以数字开头, 而变量名不能以数字开头。

TOP

这个方法去重有一个局限,批处理单个环境变量最大为8192B,所有变量的总共不能超过65536KB,如果单行字符过多、单个文件过大会有误差。

TOP


采用变量字典的去重方案有几点注意,虽是老调重弹,但用于大数据场合依然有限》
一。单个文件行数限制
二。单行字符长度限制
三。文本行皆为a-zA-Z字母时,变量字典定义因不区分大小写而失误
@echo off
for /f "delims=" %%a in ('dir /b/a-d *.txt') do (
setlocal enabledelayedexpansion
(for /f "usebackq delims=" %%i in ("%%~a") do if not defined _%%i (
set "_%%i=1" &echo,%%i))>"%%~a.new"
endlocal
)COPY

TOP

回复 10# buyiyang


    感谢,涨姿势了

TOP

回复 2# pd1


根据楼主的描述来猜测,把setlocal放到循环内部比较合适:
@echo off
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
    setlocal
    (for /f "delims=" %%i in ('type "%%~a"') do (
        if not defined _%%i (
            set "_%%i=1"
            echo,%%i
        )
    ))>"%%~a.new"
    endlocal
)COPY
1

评分人数

    • pd1: 我还没搞明白这个setlocal endlocal。 要 ...技术 + 1
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 14# Batcher


   对了, 原来可以这样, 谢谢哥哥

TOP

返回列表