使用sed替换yaml文件中的键/值

时间:2017-07-11 03:55:31

标签: linux bash awk sed

我有一个yaml文件,我需要在脚本中使用sed命令进行更新。 这是文件:

> server_options:
>     key1: value1
>     key2: value2
>     key3: value3
>     key4: value4
>     
> 
> client_options:
>     key5: value5
>     key6: value6
>     key2: value8
>     key3: value3
>     # key4: value4
>     # key7: value7

输出应为:

> server_options:
>     key1: value1
>     key2: xxx
>     key3: value3
>     key4: yyy
>     
> 
> client_options:
>     key5: value5
>     key6: value6
>     key2: value8
>     key3: value3
>     # key4: value4
>     # key7: value7

键是由标签预先修复的,我只是想更新第一次出现的key2和key4的值。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

<!doctype html>
<html>

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>

<body>
    <a href='#' onclick='downloadCSV({ filename: "youtubeData.csv" });'>Download CSV</a>
    <script type="text/javascript">
        $(document).ready(function() {});

        function convertArrayOfObjectsToCSV(args) {
            var result, ctr, keys, columnDelimiter, lineDelimiter, data;
            data = args.data || null;
            if (data == null || !data.length) {
                return null;
            }
            columnDelimiter = args.columnDelimiter || ',';
            lineDelimiter = args.lineDelimiter || '\n';
            keys = Object.keys(data[0]);
            result = '';
            result += keys.join(columnDelimiter);
            result += lineDelimiter;
            data.forEach(function(item) {
                ctr = 0;
                keys.forEach(function(key) {
                    if (ctr > 0) result += columnDelimiter;
                    result += item[key];
                    ctr++;
                    console.log("columnDelimiter " + result);
                });
                result += lineDelimiter;
            });
            return result;

        }

        function downloadCSV(args) {
            var youtubeData = [];
            var data, filename, link;

            $.getJSON('https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=PRU2ShMzQRg&key=""', function(data) {
                console.log(data);
                for(var k = 0; k < data.items.length; k++) {
                youtubeData.push({
                    "videoid": data.items[k].id,
                    "Id": data.items[k].snippet.categoryId,
                    "ChannelId": data.items[k].snippet.channelId,
                    "Title": data.items[k].snippet.title,
                    // "Description": data.items[i].snippet.description.replace("\n", ""),
                    "Commentcount": data.items[k].statistics.commentCount,
                    "ViewCount": data.items[k].statistics.viewCount,
                    "LikeCount": data.items[k].statistics.likeCount,
                    "DislikeCount": data.items[k].statistics.dislikeCount,
                    "FavoriteCount": data.items[k].statistics.favoriteCount
                });
            }
                var csv = convertArrayOfObjectsToCSV({
                    data: youtubeData
                });
                if (csv == null) return;

                filename = args.filename || 'export.csv';

                if (!csv.match(/^data:text\/csv/i)) {
                    csv = 'data:text/csv;charset=utf-8,' + csv;
                }
                data = encodeURI(csv);

                link = document.createElement('a');
                link.setAttribute('href', data);
                link.setAttribute('download', filename);
                link.click();
            });


        }
    </script>
</body>

</html>
  • sed -i '0,/^\([[:space:]]*key2: *\).*/s//\1xxx/;0,/\([[:space:]]*key4: *\).*/s//\1yyy/;' file 地址与下一个模式
  • 的第一个匹配项相匹配
  • 0:搜索并捕获零个或多个空格或制表符,后跟/^\([[:space:]]key2: \).*/
  • key2::替代匹配字符串
  • ...使用捕获的密钥和新值

第二次替换也是如此。

s//标志用于编辑文件