从.csv日志文件中的文本文件中搜索IP(如果找到),在其旁边添加新列

时间:2018-08-09 22:14:01

标签: linux bash shell csv logging

我需要一个Shell脚本,该脚本从.csv日志文件内的文本文件中查找IP地址,如果找到结果,它将在csv文件的右侧添加一个新列,称为“ SUSPICIOUS” “

到目前为止,我想出了以下命令:

<script type="text/javascript">
  var x = document.getElementById("demo");

  function pinLocn() {
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById("txtAddress").value;
    geocoder.geocode({
      'address': address
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();
        document.getElementById('LATTI').value = latitude;
        document.getElementById('LONGI').value = longitude;
        // alert(longitude + " " + latitude);
        var lat = latitude;
        var lon = longitude;
        var latlon = new google.maps.LatLng(lat, lon)
        var mapholder = document.getElementById('mapholder')
        // mapholder.style.height = '250px';
        // mapholder.style.width = '500px';

        var myOptions = {
          center: latlon,
          zoom: 14,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          mapTypeControl: false,
          navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
          }
        }

        var map = new google.maps.Map(document.getElementById("mapholder"), myOptions);
        var marker = new google.maps.Marker({
          position: latlon,
          map: map,
          title: "You are here!"
        });
      } else {
        alert("Request failed.")
      }
    });
  };

  function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else {
      x.innerHTML = "Geolocation is not supported by this browser.";
    }
  }

  function showPosition(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var latlon = new google.maps.LatLng(lat, lon)
    var mapholder = document.getElementById('mapholder')
    // mapholder.style.height = '250px';
    // mapholder.style.width = '500px';

    var myOptions = {
      center: latlon,
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false,
      navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
      }
    }

    var map = new google.maps.Map(document.getElementById("mapholder"), myOptions);
    var marker = new google.maps.Marker({
      position: latlon,
      map: map,
      title: "You are here!"
    });
  }

  function showError(error) {
    switch (error.code) {
      case error.PERMISSION_DENIED:
        x.innerHTML = "User denied the request for Geolocation."
        break;
      case error.POSITION_UNAVAILABLE:
        x.innerHTML = "Location information is unavailable."
        break;
      case error.TIMEOUT:
        x.innerHTML = "The request to get user location timed out."
        break;
      case error.UNKNOWN_ERROR:
        x.innerHTML = "An unknown error occurred."
        break;
    }
  }
</script>

这有两个问题:

  1. 它在每一行上创建SUSPICIOUS列

  2. 它将在左侧而不是右侧创建新列

(如果我使用正则grep表达式,则只会保存包含这些IP地址的结果,我需要日志的每一行)

非常感谢您抽出宝贵的时间阅读本文并提供帮助。

1 个答案:

答案 0 :(得分:0)

使用awk似乎会容易得多。将文本文件加载到关联数组的键中。然后读取CSV文件,并测试数组中是否包含IP字段。

awk -i inplace -F, -v OFS=, '
    NR==FNR { a[$0]++; next } # Put lines from list.txt into array
    a[$8] { print $0 "SUSPICIOUS" } # Test if IP in current row is in array
    { print }' inplace=0 list.txt inplace=1 *.csv # Otherwise print row normally

这使用GNU awk的inplace扩展名,因此它可以将结果写回到输入文件中。