使用sed清理SchemaSync输出

时间:2019-04-02 06:13:33

标签: sed

目前,我已经创建了一个小样例。我试图正确地引用DEFAULT值,特别是#2 DEFAULT ONE值应引用为DEFAULT 'ONE'

#1 CREATE TABLE `table` (`column` int(10) unsigned DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
#2 ALTER TABLE `table` MODIFY COLUMN `column2` enum('ONE','TWO') NOT NULL DEFAULT ONE AFTER `column1`;
#3 ALTER TABLE `table` MODIFY COLUMN `column` varchar(64) NOT NULL DEFAULT '' FIRST;

我目前针对上述行使用以下行,以修复第2行:

sed "s/DEFAULT \([a-zA-Z0-9_.]*\)/DEFAULT '\1'/g"

这是我得到的输出:

#1 CREATE TABLE `table` (`column` int(10) unsigned DEFAULT 'NULL') ENGINE=InnoDB DEFAULT 'CHARSET'=utf8mb4 COLLATE=utf8mb4_unicode_ci;
#2 ALTER TABLE `table` MODIFY COLUMN `column2` enum('ONE','TWO') NOT NULL DEFAULT 'ONE' AFTER `column1`;
#3 ALTER TABLE `table` MODIFY COLUMN `column` varchar(64) NOT NULL DEFAULT '''' FIRST;

如您所见,它修复了#2行,但是#1&#3现在有问题。

#1 DEFAULT 'NULL'    (should remain DEFAULT NULL)
#1 DEFAULT 'CHARSET' (should remain DEFAULT CHARSET)
#2 DEFAULT 'ONE'     (GOOD!)
#3 DEFAULT ''''      (should remain DEFAULT '')

是否有一种方法可以调整sed以忽略诸如DEFAULT NULLDEFAULT CHARSETDEFAULT ''之类的特定模式?

sed "s/DEFAULT (not followed by NULL|CHARSET|'')\([a-zA-Z0-9_.]*\)/DEFAULT '\1'/g"

或者也许有更好的方法?

谢谢!

3 个答案:

答案 0 :(得分:2)

如果您选择app.config(function ($stateProvider, $urlRouterProvider) { $stateProvider .state('app.quiz', { url: '/quiz', views: { 'menuContent': { templateUrl: 'templates/quiz/quizlists.html', controller: 'quizListsCtrl' } } }) .state('app.quizinfo', { url: '/quiz/info', views: { 'menuContent': { templateUrl: 'templates/quiz/quizinfo.html', controller: 'quizInfoCtrl' } }, params: { quizid: null } }) .state('app.quizexe', { url: '/quiz/exe', views: { 'menuContent': { templateUrl: 'templates/quiz/quizexe.html', controller: 'quizExeCtrl' } }, params: { quizid: null } }) .state('app.quizfinish', { url: '/quiz/finished', views: { 'menuContent': { templateUrl: 'templates/quiz/quizfinish.html', controller: 'quizFinishCtrl' } } }); $urlRouterProvider.otherwise('/app/home'); ,请尝试:

<apex:page >
  <apex:includeScript value="{!$Resource.Chartjs}"/>
  <script language="JavaScript">
   window.onload = function displayLineChart() {
    var data = {
        labels: [669.426, 669.427,735.618,753.170,801.809],
        datasets: [
            {

                fillColor: "rgb(255,255,255)",
                strokeColor: "rgb(0,0,128,1.0)",
                pointColor: "rgba(176,196,222)",
                borderColor: "lightgreen",



                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [0.00, 50, 100, 126.52, 200]
            },

        ]
    };
    var ctx = document.getElementById("lineChart").getContext("2d");
    var options = { 

            scale: {
   ticks: {
      display: false
   }
}
            };
    var lineChart = new Chart(ctx).Line(data, {

    //Boolean - If we show the scale above the chart data           
    scaleOverlay : false,

    //Boolean - If we want to override with a hard coded scale
    scaleOverride : false,

    //** Required if scaleOverride is true **
    //Number - The number of steps in a hard coded scale
    scaleSteps : null,
    //Number - The value jump in the hard coded scale
    scaleStepWidth : null,
    //Number - The scale starting value
    scaleStartValue : null,

    //String - Colour of the scale line 
    scaleLineColor : "rgba(0,0,0,.1)",

    //Number - Pixel width of the scale line    
    scaleLineWidth : 2,

    //Boolean - Whether to show labels on the scale 
    scaleShowLabels : false,

    //Interpolated JS string - can access value
    scaleLabel : "<%=value%>",

    //String - Scale label font declaration for the scale label
    scaleFontFamily : "'Arial'",

    //Number - Scale label font size in pixels  
    scaleFontSize : 12,

    //String - Scale label font weight style    
    scaleFontStyle : "normal",

    //String - Scale label font colour  
    scaleFontColor : "#666",    

    ///Boolean - Whether grid lines are shown across the chart
    scaleShowGridLines : false,

    //String - Colour of the grid lines
    scaleGridLineColor : "rgba(0,0,0,.05)",

    //Number - Width of the grid lines
    scaleGridLineWidth : 1, 

    //Boolean - Whether the line is curved between points
    bezierCurve : true,

    //Boolean - Whether to show a dot for each point
    pointDot : true,

    //Number - Radius of each point dot in pixels
    pointDotRadius : 5,

    //Number - Pixel width of point dot stroke
    pointDotStrokeWidth : 1,

    //Boolean - Whether to show a stroke for datasets
    datasetStroke : true,

    //Number - Pixel width of dataset stroke
    datasetStrokeWidth : 2,

    //Boolean - Whether to fill the dataset with a colour
    datasetFill : true,

    //Boolean - Whether to animate the chart
    animation : true,

    //Number - Number of animation steps
    animationSteps : 60,

    //String - Animation easing effect
    animationEasing : "easeOutQuart",

    //Function - Fires when the animation is complete
    onAnimationComplete : null

});
      lineChart.defaults.scale.gridLines.display 


            = false;

  }
</script>
    <div class="box">
    <canvas id="lineChart" height="500" width="600"></canvas>
  </div>

</apex:page>

输出:

Perl

答案 1 :(得分:1)

尝试:

sed -E "s/DEFAULT (NULL|CHARSET)/DEFAULT_\1/g; s/DEFAULT ([[:alnum:]_.]+)/DEFAULT '\1'/g; s/DEFAULT_(NULL|CHARSET)/DEFAULT \1/g" file

这可以通过三个步骤进行:

  1. s/DEFAULT (NULL|CHARSET)/DEFAULT_\1/g

    这将隐藏您不想更改的默认值。

  2. s/DEFAULT ([[:alnum:]_.]+)/DEFAULT '\1'/g

    这将更改您要更改的值。

    请注意,我将*更改为+。这意味着空字符串将不匹配。这解决了第3行中的问题。

    我也将[a-zA-Z0-9]更改为[:alnum:],以便正则表达式将以Unicode安全的方式匹配所有字母数字字符。 (如果这不是您想要的,只需将这部分改回来。)

  3. s/DEFAULT_(NULL|CHARSET)/DEFAULT \1/g

    这会改回您不想更改的内容。

此方法假定DEFAULT_NULLDEFAULT_CHARSET都没有出现在您的实际输入中。根据您到目前为止的显示,这似乎是一个安全的假设。

示例

使用您的输入文件:

$ cat file
#1 CREATE TABLE `table` (`column` int(10) unsigned DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
#2 ALTER TABLE `table` MODIFY COLUMN `column2` enum('ONE','TWO') NOT NULL DEFAULT ONE AFTER `column1`;
#3 ALTER TABLE `table` MODIFY COLUMN `column` varchar(64) NOT NULL DEFAULT '' FIRST;

我们的命令产生:

$ sed -E "s/DEFAULT (NULL|CHARSET)/\n\1/g; s/DEFAULT ([[:alnum:]_.]+)/DEFAULT '\1'/g; s/\n(NULL|CHARSET)/DEFAULT \1/g" file
#1 CREATE TABLE `table` (`column` int(10) unsigned DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
#2 ALTER TABLE `table` MODIFY COLUMN `column2` enum('ONE','TWO') NOT NULL DEFAULT 'ONE' AFTER `column1`;
#3 ALTER TABLE `table` MODIFY COLUMN `column` varchar(64) NOT NULL DEFAULT '' FIRST;

答案 2 :(得分:0)

使用GNU awk进行多字符RS:

awk -v RS='DEFAULT \\w+' -v ORS= '
    RT { split(RT,rt); if (rt[2] !~ /^(NULL|CHARSET)$/) RT=rt[1]" \047"rt[2]"\047" }
    { print $0 RT }
' file
#1 CREATE TABLE `table` (`column` int(10) unsigned DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
#2 ALTER TABLE `table` MODIFY COLUMN `column2` enum('ONE','TWO') NOT NULL DEFAULT 'ONE' AFTER `column1`;
#3 ALTER TABLE `table` MODIFY COLUMN `column` varchar(64) NOT NULL DEFAULT '' FIRST;