在10000个文件中搜索并重复一个字符串

时间:2016-11-02 09:14:58

标签: java bash

编辑:我有10,000个相同的文件,其中包含以下文字。

TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting

我的要求是在10 k文件中替换如下。

  1. 90936266600000000019093626660000010000的TRANSACTION_ID。 和
  2. 来自VUpldr_QA_1Mb_Report00001的VAULT_REPORT_NAME VUpldr_QA_1Mb_Report10000
  3. 所以我的输出文件内容是

      

    档案第一名:

    TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
    
      

    file 2nd:

    TRANSACTION_ID=9093626660000000002,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00002,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
    
      

    文件10000th:

    TRANSACTION_ID=9093626660000010000,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report10000,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting

    我写了下面的代码,但那不起作用:

    import java.io.*;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class mdScript {
    
    
    
    
        public static void main(String[] args) throws IOException {
    
            for (int i=1;i<=10000;i++){
    
            StringBuffer pathVarBuf = new StringBuffer();
            pathVarBuf.append("/Users/564169/Desktop/Vault_Testing/vUpldr/MD/1MbReport");
            pathVarBuf.append(i);
            pathVarBuf.append(".md");
            //System.out.println(pathVarBuf);
            Path path = Paths.get(pathVarBuf.toString());
            Charset charset = StandardCharsets.UTF_8;
    
            String content = new String(Files.readAllBytes(path), charset);
            content = content.replaceAll("VUpldr_QA_1Mb_Report00001", "RVUpldr_QA_1Mb_Report"+i);
            int id=999900001; // (Transaction id in the org MD file is 90)
            id = id+i;
            content = content.replaceAll("999900001", Integer.toString(id));
            Files.write(path, content.getBytes(charset));
            System.out.println(content);
    
            }
    
    }
    
    }
    

    我收到以下错误:

    Exception in thread "main" java.lang.NullPointerException
        at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:267)
        at java.nio.file.Paths.get(Paths.java:84)
        at mdScript.main(mdScript.java:22)
    

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。你也可以通过编程方式完成它。

简单的解决方案将使用TextPad 这是相同的链接 TextPad find and replace in files