找到字符串匹配的特定列

时间:2017-09-29 06:19:19

标签: bash awk grep

我有一个文件test.sh.在这里我的内容看起来像

Nas /mnt/enjayvol1/backup/test.sh lokesh
thinclient rsync /mnt/enjayvol1/esync/lokesh.sh lokesh
crm rsync -arz --update /mnt/enjayvol1/share/mehul mehul mehul123

我想检索匹配content / mnt

的字符串

我想要输出线

/mnt/enjayvol1/backup/test.sh
/mnt/enjayvol1/esync/lokesh.sh
/mnt/enjayvol1/share/mehul

我试过了

grep -i "/mnt" test.sh | awk -F"mnt" '{print $2}'

但这不会给我准确的输出。请帮忙

2 个答案:

答案 0 :(得分:2)

你能不能尝试一下awk方法,让我知道这是否对你有帮助。

awk -v RS=" " '$0 ~ /\/mnt/'   Input_file

输出如下。

/mnt/enjayvol1/backup/test.sh
/mnt/enjayvol1/esync/lokesh.sh
/mnt/enjayvol1/share/mehul

说明:将记录分隔符作为空格,然后检查是否有任何行包含/ mnt字符串,如果是,则不提及任何操作,因此默认打印将会发生。因此它将打印那些在其中有/ mtn刺痛的行。

答案 1 :(得分:1)

grep 方法(假设 / mnt ... 路径不包含空格):

<?php

include "db.php";


    /* lat/lng data will be added to this array */
    $locations=array();
    $query =  $db->query("SELECT * FROM ds_petroling_history");
        while( $row = $query->fetch_assoc() ){

            $nama_kabkot = $row['name'];
            $longitude = $row['lng'];                              
            $latitude = $row['lat'];

            /* Each row is added as a new array */
            $locations[]=array( 'name'=>$nama_kabkot, 'lat'=>$longitude, 'lng'=>$longitude );
        }
        /* Convert data to json */
        $markers = json_encode($locations);
?>


<html>
    <head>


    </head>
    <body>

       <div><?php echo $markers ?></div> 


     <script type='text/javascript'>
    <?php
        echo "var markers=$markers;\n";

    ?>

    function initMap() {

        var latlng = new google.maps.LatLng(23.0117523,72.5226665);
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false
        };

        var map = new google.maps.Map(document.getElementById("peta"),myOptions);
        var infowindow = new google.maps.InfoWindow(), marker, lat, lng;

        var json=JSON.parse( markers );

        for( var o in json ){

            lat = json[ o ].lat;
            lng=json[ o ].lng;
            name=json[ o ].name;

            marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat,lng),
                name:name,
                map: map
            }); 
            google.maps.event.addListener( marker, 'click', function(e){
                infowindow.setContent( this.name );
                infowindow.open( map, this );
            }.bind( marker ) );
        }
    }
    </script>  
       <div id="peta" style="width: 100%; height: 660px;">
       </div>

    <script async defer src="https://maps.googleapis.com/maps/api/js?key=yourkey&callback=initMap"
  type="text/javascript"></script>
    </body>
</html>

输出:

grep -o '\/mnt\/[^[:space:]]*' lokesh.sh