Board logo

标题: [文本处理] bat脚本怎样判断文本 提取指定文本? [打印本页]

作者: idc878787    时间: 2022-3-24 17:47     标题: bat脚本怎样判断文本 提取指定文本?

bat判断文本 提取指定文本???

例如:文本a.txt

a----ba----c----d
a----b----ca----d
a----d----c-----d
a----b----cA----d
a----bA----c----d
我们----哈我们-----c----d
购买----哈-----c购买----d
辅导----地方----发发----d

生成b.txt

a----ba----c----d
a----b----ca----d
a----b----cA----d
a----bA----c----d
我们----哈我们-----c----d
购买----哈-----c购买----d


就是:1----2----3----4     四部分  2和3里面 如果任何一个部分 包含 部分1 就记录b.txt   英文要大小写忽略
作者: Batcher    时间: 2022-3-25 08:44

回复 1# idc878787


    在这个例子里面 a----d----c-----d 这一行不符合哪个要求被删掉了?
作者: qixiaobin0715    时间: 2022-3-25 09:11

本帖最后由 qixiaobin0715 于 2022-3-25 09:17 编辑

发现有很多求助帖子中例子太随意,大多都是自己凭空编写的且不典型。有人根据提供的例子写了代码,就会说这里不行那里不行,实际上却是他自己没有说清楚。提供的例子或示范文本最好是实际工作中真实的东西或片段,这样才会尽可能避免出现诸多问题。
作者: for_flr    时间: 2022-3-25 09:41

回复 3# qixiaobin0715

楼主是这个问题
for /f "tokens=1,2,3,4 delims=-" %%a in (a.txt) do (
忽略大小写,判断%%b和%%c中是否包含字符串%%a,有则写进b.txt
)
我在想,不用findstr的情况下,批处理怎么写
作者: qixiaobin0715    时间: 2022-3-25 09:58

回复 4# for_flr
我想是这样吧:
  1. set str1=%%b
  2. set str2=%%c
  3. if /i not "!str1:%%a=!" =="!str1!" (
  4. echo,...
  5. ) else if /i not "!str2:%%a=!" =="!str2!" (
  6. echo,...
  7. )
复制代码

作者: qixiaobin0715    时间: 2022-3-25 10:03

回复 4# for_flr
这样呢
  1. set str=%%b%%c
  2. if /i not "!str:%%a=!" =="!str!" echo,...
复制代码

作者: Batcher    时间: 2022-3-25 12:01

回复 1# idc878787


请参考Q-04和Q-05把bat文件和txt文件都保存为ANSI编码:
https://mp.weixin.qq.com/s/6lbb97qUOs1sTyKJfN0ZEQ
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. (for /f "tokens=1-4 delims=-" %%a in ('type "a.txt"') do (
  5.     set "str=%%b%%c"
  6.     if /i "!str:%%a=!" neq "!str!" (
  7.         echo %%a----%%b----%%c----%%d
  8.     )
  9. ))>"b.txt"
复制代码

作者: idc878787    时间: 2022-3-25 13:26

3q   
作者: idc878787    时间: 2022-12-19 10:35

回复 7# Batcher


    有没有更快的处理方式,这个百万数据处理1天都还没好?
作者: hfxiang    时间: 2022-12-19 11:51

如果数据量很大,建议用gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe ),效率会很高
执行以下指令之前,请确保a.txt为ANSI编码
  1. gawk -F"-+" -vIGNORECASE=1 "{if($2~$1||$3~$1)print}" a.txt>b.txt
复制代码

作者: idc878787    时间: 2022-12-19 12:55

本帖最后由 idc878787 于 2022-12-19 13:22 编辑

回复 10# hfxiang


     数据中  如果含有这个符号  就会丢数据?
作者: terse    时间: 2022-12-19 14:47

会不会有下面这个情况呢 就是数据第一段不是个位数 例如 %%a=XX
如此情况下,上面代码应该出错吧
作者: idc878787    时间: 2022-12-19 15:23

回复 12# terse


    7楼的代码都可以   就是处理数据多的时候   慢        10楼的代码就是 处理文本含有    这符号的 时候  就丢数据了  不处理了
作者: hfxiang    时间: 2022-12-19 17:00

回复 11# idc878787


   
请确保a.txt为ANSI编码格式,该脚本方能生效
作者: hfxiang    时间: 2022-12-19 17:44

本帖最后由 hfxiang 于 2022-12-19 18:03 编辑

回复 11# idc878787


    经测试,如果含“”字符,需要用 Ruby(https://rubyinstaller.org/downloads/)中附带的 gawk 方能有效处理(脚本不变)
作者: terse    时间: 2022-12-19 18:06

数据量大 这个估计也够呛 试试吧
  1. 1>1/* :
  2. @echo off
  3. cscript -nologo -e:jscript "%~f0"<a.txt >b.txt
  4. pause&exit
  5. */
  6. while (!WScript.StdIn.AtEndOfStream)
  7. {
  8.        var text = WScript.StdIn.ReadLine(),
  9.        ar = text.replace(/-+/g,'-').split('-'),
  10.        re = new RegExp(ar[0],"i");
  11.        if (re.test(ar[1]) | re.test(ar[2]) ) { WSH.Echo( text )};
  12. }
复制代码

作者: idc878787    时间: 2022-12-20 00:12

回复 15# hfxiang


    下载哪个   有点蒙
作者: idc878787    时间: 2022-12-20 00:12

回复 16# terse


    百万数据这个慢
作者: tmplinshi    时间: 2022-12-20 01:27

本帖最后由 tmplinshi 于 2022-12-20 07:39 编辑

试试 PowerShell
  1. function ParseFile {
  2.     param (
  3.         [Parameter(Mandatory = $true)][Alias("in")] $inputFile,
  4.         [Parameter(Mandatory = $true)][Alias("out")] $outputFile
  5.     )
  6.     if (-not (Test-Path $inputFile)) {
  7.         Write-Error "文件不存在: $inputFile"
  8.         return
  9.     }
  10.    
  11.     $streamIn  = [IO.StreamReader]::new($inputFile)
  12.     $streamOut = [IO.StreamWriter]::new($outputFile, $false, [System.Text.ASCIIEncoding]::UTF8)
  13.     while (-not $streamIn.EndOfStream) {
  14.         $line = $streamIn.ReadLine()
  15.         $arr = $line -split '-+'
  16.         if ($arr[1, 2] -join '-' -like "*$($arr[0])*") {
  17.             $streamOut.WriteLine($line)
  18.         }
  19.     }
  20.    
  21.     $streamIn.close()
  22.     $streamOut.close()
  23. }
  24. ParseFile -in a.txt -out b.txt
复制代码

作者: terse    时间: 2022-12-20 15:34

如果装有python的话 能稍微提升一点效率
  1. @python -x "%~f0" >b.txt& pause &exit
  2. # -*- coding: utf-8 -*-
  3. import os,re
  4. file = r'a.txt'
  5. with open(file) as f:
  6.     for line in f.readlines():
  7.          arr = re.split('\.+',line)
  8.          if arr[0].lower() in '-'.join(arr[1:3]).lower():
  9.             print( line.strip())
复制代码

作者: hfxiang    时间: 2022-12-20 17:34

回复 17# idc878787


下载WITH DEVKIT版本
即:https://github.com/oneclick/ruby ... kit-3.1.3-1-x64.exe
或:https://github.com/oneclick/ruby ... kit-3.1.3-1-x86.exe
安装后才会有gawk.exe




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