0

我正在寻找通过Windows批处理文件或vbscript将文件A分成两个(文件B和文件C)的方法。如果您能提供示例代码,我将不胜感激!

File A
------------------------------
'H',123,'1st'
'D',123,'1st'
'D',123,'2nd'
'D',123,'3rd'
'H',456,'2nd'
'D',456,'1st'
'D',456,'2nd'
'D',456,'3rd'
------------------------------

File B
------------------------------
'H',123,'1st'
'H',456,'2nd'
------------------------------

File C
------------------------------
'D',123,'1st'
'D',123,'2nd'
'D',123,'3rd'
'D',456,'1st'
'D',456,'2nd'
'D',456,'3rd'
------------------------------
4

3 回答 3

2
findstr /bl "'H'" a.txt >b.txt
findstr /bl "'D'" a.txt >c.txt
于 2012-09-20T20:40:24.120 回答
0

出于完整性或教育目的,VBS 解决方案。
每个找到的字母的输出流对象作为值保存在字典中。

Option Explicit
Dim fso, dic, ts_in, ts_out, s, key, INFILE, OUTDIR
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set dic = CreateObject("scripting.dictionary")

INFILE = "c:\temp\infile.txt"
OUTDIR = Left (INFILE, InstrRev (INFILE, "\"))  'same folder as INFILE's

Set ts_in = fso.OpenTextFile (INFILE)
Do Until ts_in.AtEndOfStream
    s = ts_in.ReadLine
    key = Replace (Left (s, InStr (s, ",")-1), "'", "")
    If Not dic.Exists (key) Then
        Set ts_out = fso.CreateTextFile (OUTDIR & key & ".txt")
        dic.Add key, ts_out
    End If
    Set ts_out = dic(key)
    ts_out.writeLine s
Loop
ts_in.Close

For Each ts_out In dic.Items
    ts_out.Close
Next
于 2012-09-24T19:30:27.090 回答
0

您也可以像这样以编程方式执行此操作:

for /f "tokens=1-3 delims=," %%a in (File_A) do (
  if "%%a"=="'H'" echo %%a,%%b,%%c>>File_B
  if "%%a"=="'D'" echo %%a,%%b,%%c>>File_C
)

我相信这种方法可能会更慢,但它可以让您更精细地调整您的条件或操作数据,而无需学习 REGEX(FINDSTR实现很差)。

于 2012-09-21T02:35:50.280 回答