如何使用正则表达式grep缩进段落的行?

时间:2019-12-18 20:14:48

标签: regex grep

我有一个docker-compose文件,我想从该文件中提取“ environment:”部分中的环境变量。

        image: non-important
        environment:
            DATABASE_URL: postgresql://localhost:5234
            REDIS_URL: 'redis://redis:6379'
            PRODUCTION_URL: 'http://localhost:4000'
            CLIENT_URL: 'http://localhost:3000'
            TOKEN_SEND_MODE: 'cookie|query_param'
        other_service:
            ...

有没有办法只在“ environment:”和“ other_service:”之间对那些变量进行grep? 与其他行的唯一大不同是它们比其他行更缩进,但是我不知道使用正则表达式将它们与其他行区分开。

1 个答案:

答案 0 :(得分:1)

使用

    import Data from "./BookOfBusiness.json";

    const initialState = {
    TotalData: Data
    };

    const filterDataWTF = (state = initialState, action) => {
    let newState = { ...state };
    let itExport = newState.TotalData;


    if (action.type === "Status55") {
        let itExport22 = {...itExport};
        console.log("came to Status55 and itExport value is " + itExport22); // comes undefined. WHY??

        return itExport22.filter(xyz => {
        if (xyz.Status55.includes("Submitted")) {
            return true;
        }

        return false;
        });
    }

    return itExport;
    };

    export default filterDataWTF;

输出

perl -0 -pe 's/.*(\n\s*DATABASE_URL:.*?)\bother_service:/$1/s' file