Perl替换文件中特定块内的字符串

时间:2017-09-16 19:19:59

标签: perl findandmodify

嗨我正在尝试用文件替换文件中的字符串test.txt:

import Promise from 'bluebird';
import request from 'superagent';

// sends a post request to server 
const servercall2 = (args, response) => {
    const promise = new Promise((resolve, reject) => {

        const req = request
            .post(`${baseUrl}/def`)
            .send(args, response)
            .setAuthHeaders();

        req.endAsync()
            .then((res) => resolve(res))
            .catch((err) => reject(err));
    });

    return promise;
};

// sends a post request to server
const servercall1 = (args) => {
    const promise = new Promise((resolve, reject) => {

        const req = request
            .post(`${baseUrl}/abc`)
            .send(args)
            .setAuthHeaders();

        req.endAsync()
            .then((res) => resolve({res}))
            .catch((err) => reject(err));
    });

    return promise;
};


// function to send request to cgi server to execute actions from ui
async function makeServerCalls(args, length) {

    // convert args to two dimensional array, chunks of given length [[1,2,3], [4,5,6,], [7,8]]

    const batchedArgs = args.reduce((rows, key, index) => (index % length === 0 ? rows.push([key])
        : rows[rows.length - 1].push(key)) && rows, []);

    const responses = [];

    for (const batchArgs of batchedArgs) {
        responses.push(
            // wait for a chunk to complete, before firing the next chunk of calls
            await Promise.all(

                ***// Error, expected to return a value in arrow function???***
                batchArgs.map((args) => {
                    const req = request
                        .get(`${baseUrl}/pqr`)
                        .query(args)

                    // I want to collect response from above req at the end of all calls.
                    req.endAsync()
                        .then((response) =>servercall2(args,response))
                        .then((res) => res);
                })
            )
        );
    }

    // wait for all calls to finish
    return Promise.all(responses);
}

export function execute(args) {
    return (dispatch) => {

       servercall1(args)
           .then(makeServerCalls(args, 3))
           .then((responses) => {
                    const serverresponses = [].concat(...responses);
                    console.log(serverresponses);
            });
    };
}

并将其转换为

  <g
   id="g16526">
  <g

  <g
   id="gnnnnn">
  <g

使用此perl脚本

  <g
   id="gg1">
  <g
   ...
  <g
   id="ggn">
  <g

但它不起作用,任何建议赞赏

1 个答案:

答案 0 :(得分:1)

如果确实具有类似XML的结构,则应使用XML::LibXMLXML::Twig的模块进行处理。

但是,所显示的这项任务也很容易以基本的方式完成

perl -0777 -wpE'
    BEGIN { $cnt = 0 };
    s/<g\nid="g\K(.*?)"/q(g).(++$cnt).q(")/eg;
' input.txt

期望文件格式完全如图所示。它通过-0777将整个文件读入一个字符串,这些文件不是最漂亮的,可能不适合非常大的文件。

另一种方法是将记录分隔符设置为<g,因此每个“行”都是要处理的块

perl -wpE'
    BEGIN { local $/ = "<g"; $cnt = 0 }; 
    s/id="g\K(.*?)"/q(g).++$cnt.q(")/eg; 
' input.txt

现在正则表达式可以自由地寻找id="...",我们可以逐行处理。

这两个都打印预期的输出。为了便于测试,它们是单行的,我建议转移到脚本。