如何在第一行和前两列添加引号(csv-命令行)

时间:2018-07-17 15:45:38

标签: csv unix sed command-line

我有点挣扎,因为我需要在csv文件的第一行和前两列添加双引号,如下所示:

输入

var1,var2,var3,var4   
x1,x2,x3,x4  
y1,y2,y3,y4  

输出:

"var1","var2","var3","var4"  
"x1","x2",x3,x4  
"y1","y2",y3,y4  

我已经尝试了一段时间,最近发现的是:

# add single quotes around columns of csv files
sed "s/[[:alnum:]]*/'&'/g" input.csv >output1.csv
# transform the single quotes into double quotes
tr "'" '"' < output1.csv > output2.csv

但这会在所有列上加上引号,如下所示:

"var1","var2","var3","var4"  
"x1","x2","x3","x4"  
"y1","y2","y3","y4" 

您,更有经验的命令行用户可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以限定替换特定的行号

<?php

    $outerClassName='myClass';
    $innerClassName='clsInner';

    $attrib='class';

    ob_start();
?>

<html>
    <head>
        <title>Manipulating the output</title>
    </head>
    <body>
        <div class="clsA">
          <div>
            <div class="clsB">
              Content (can be anything including clsA, clsB etc.)
            </div>
          </div>
        </div>

        <div class="clsC">
          <div class="clsB">
              Content (can be anything including clsC, clsB etc.)
          </div>
        </div>
    </body>
</html>
<?php
    $dom=new DOMDocument;
    $dom->loadHTML( ob_get_contents() );
    $xp=new DOMXPath( $dom );
    $col = $xp->query( '//div' );



    if( $col->length > 0 ){
        foreach( $col as $node ){

            if( !stristr( $node->getAttribute( $attrib ), $outerClassName ) ) {
                $children=$xp->query( 'div', $node );

                if( $children->length > 0 ){
                    if( $node->hasAttribute( $attrib ) ) $node->setAttribute( $attrib, trim( $node->getAttribute( $attrib ) . ' ' . $outerClassName ) );
                } else {
                    if( $node->hasAttribute( $attrib ) ) $node->setAttribute( $attrib, trim( $node->getAttribute( $attrib ) . ' ' . $innerClassName ) );
                }
            }
        }
        ob_clean();
        echo $dom->saveHTML();
    }


    $dom = $xp = $col = $children = null;
    ob_end_flush();
?>
  • $ sed '1s/[^,]*/"&"/g; 2,$s/\([^,]*\),\([^,]*\)/"\1","\2"/' ip.txt "var1","var2","var3","var4" "x1","x2",x3,x4 "y1","y2",y3,y4 在第一行的所有字段添加引号
  • 1s/[^,]*/"&"/g仅对从第二行到文件结尾的前两个字段添加引号