使用awk提取修改的文件内容

时间:2018-02-07 03:51:24

标签: bash awk

我有2个文件,其中包含超过1000万行,我想以有效的方式比较这2个文件,在我的情况下,我试图使用 awk 获得以下答案,我是坚持获得修改内容部分

Sample FIle (a1.txt)
A,1
B,2
C,3
D,4

Sample FIle (b1.txt)   
B,2
C,55 <- Mod data
D,4
E,5  <- new data
F,6  <- new data
Z,11 <- new data

* ** A,1删除和修改仅适用于第2列

这是我的代码和预期输出

新增内容 - 工作正常

awk -F',' 'NR==FNR{++a[$1];next} {line++;if(!(a[$1])){print $0}}' a1.txt b1.txt 

E,5
F,6
Z,11

已删除的内容 - 正常运行

awk -F',' 'NR==FNR{++a[$1];next} {line++;if(!(a[$1])){print $0}}' b1.txt a1.txt

A,1

修改内容

awk -F',' 'NR==FNR{++a[$2];next} {line++;if(!(a[$2])){print $0}}' a1.txt b1.txt

当前输出(不需要)

C,55
E,5
F,6
Z,11

但需要输出

C,55

这将返回bot新增内容+修改内容

请帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="robots" content="noindex, nofollow">
    <meta name="googlebot" content="noindex, nofollow">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/result-light.css">
    <style type="text/css"></style>
    <title></title>

    <script type='text/javascript'>
        //<![CDATA[
        jQuery(function ($) {
            // Asynchronously Load the map API 
            var script = document.createElement('script');
            script.src =
                "http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false&callback=initialize";
            document.body.appendChild(script);
        });
        function initialize() {
            var map;
            var bounds = new google.maps.LatLngBounds();
            var mapOptions = {
                mapTypeId: 'roadmap'
            };
            // Display a map on the page
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            map.setTilt(45);
            // Multiple Markers
            var markers = [
                ['Anuradhapura', 8.3372688, 80.3619399, '#1'],
                ['Sigiriya', 7.9568242, 80.7434198, '#2'],
                ['Hotel See Kandy', 7.2930541, 80.6159102, '#3']
            ];
            // Display multiple markers on a map
            var infoWindow = new google.maps.InfoWindow(), marker, i;
            // Loop through our array of markers & place each one on the map  
            for (i = 0; i < markers.length; i++) {
                var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
                bounds.extend(position);
                marker = new google.maps.Marker({
                    position: position,
                    map: map,
                    title: markers[i][0],
                    url: markers[i][3]
                });
                google.maps.event.addListener(marker, 'click', function () {
                    console.log('click');
                    // console.log($(marker.url));
                    var elem = $(marker.url);
                    console.log(elem);
                    // alert(elem);

                    $('html, body').animate({
                        scrollTop: elem.offset().top
                    }, 1000);
                });
                // Automatically center the map fitting all markers on the screen
                map.fitBounds(bounds);
            }
            // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
            var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
                this.setZoom(8);
                google.maps.event.removeListener(boundsListener);
            });
        }
        //]]>
    </script>

</head>

<body>
    <div id='map_canvas' style="width: 100%;height: 100vh"></div>
    <div class="location-details" id='1'>1</div>
    <div class="location-details" id='2'>2</div>
    <div class="location-details" id='3'>3</div>

    <script>
        // tell the embed parent frame the height of the content
        if (window.parent && window.parent.parent) {
            window.parent.parent.postMessage(["resultsFrame", {
                height: document.body.getBoundingClientRect().height,
                slug: "o8u13whg"
            }], "*")
        }
    </script>

    <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>

</body>

<style>
    .location-details {
        background-color: blue;
        color: white;
        width: 100%;
        display: inline-block;
        height: 100vh;
    }
    .location-details:nth-child(odd) {
        background-color: red;
    }
</style>

</html>