【问题标题】:Need shell / perl script to mask sensitive info like first name, dob, ssn etc in a log file on linux需要 shell / perl 脚本来屏蔽 Linux 日志文件中的名字、dob、ssn 等敏感信息
【发布时间】:2014-07-28 20:09:44
【问题描述】:

我需要在日志文件中屏蔽名字、姓氏、dob、ssn 等敏感信息,但它们没有特定的显示模式。需要在整个日志中找到类似下面的字段,并用 xxxxx 屏蔽信息。请帮忙。

带有样本数据的日志块

ghix.log.2014-07-25: INFO 07/25/2014 17:13:14 (PlanDisplayRestClient.java:272) - Fetching  IndividualPlanList : {"inputData":{"household":{"CSR":"CS4","APTC":"187.0"},"issuerVerifiedFlag":true,"totalContribution":null,"enrollmentType":"I","exchangeType":"ON","showCatastrophicPlan":"false","issuerId":null,"groupId":3098,"eligLeadBenefits":",Nutritional counseling,Weight loss programs","subscriberData":null,"planIdStr":"","planType":"Both","tenant":"","providers":[{"id":"18900405","name":"Dr. Rakshit Kumar","networkId":["33602-TXN001","87006-TXN001","91986-TXN001","32600-TXN001","32600-TXN002","355678-FLN001"],"networkTier":"","spciality":"Counseling/Social Work","city":"Austin","state":"TX","providerType":"DOCTOR","networkTierList":null,"networkIdList":["33602-TXN001","87226-TXN001","91716-TXN001","3278-TXN001","32698-TXN002","3545-FLN001"]}],"planLevel":"","isSpecialEnrollment":"NO","pgrmType":"INDIVIDUAL","coverageStartDate":"01/01/2001","insuranceType":"HEALTH","preferences":{"highDrugUseVal":0.0,"lowDrugUseVal":0.0,"moderateMedicalVal":0.0,"highMedicalVal":0.0,"vHighDrugUseVal":0.0,"moderateDrugUseVal":0.0,"vHighMedicalVal":0.0,"lowMedicalVal":0.0}},"groupDataList":[{"groupId":3098,"aptc":187.0,"remainingAptc":0.0,"csr":"CS4","zipcode":"44444","countycode":"45555","personDataList":[{"personId":"1","externalPersonId":null,"existingMedicalEnrollmentID":null,"existingSADPEnrollmentID":null,"firstname":"Primary","lastname":"Tax Filer","dob":"1/1/2001","smoker":"N","dentalEligible":"NO","relationship":"Self","employerContribution":null,"gender":null},{"personId":"2","externalPersonId":null,"existingMedicalEnrollmentID":null,"existingSADPEnrollmentID":null,"firstname":"Primary","lastname":"Tax Filer","dob":"1/1/2001","smoker":"N","dentalEligible":"NO","relationship":"Child","employerContribution":null,"gender":null}]}],"pldHouseholdPersonList":null,"providersList":[{"id":"1000405","name":"Dr. Rakshit Kumar","networkId":["33002-TXN001","87000-TXN001","91000-TXN001","30003-TXN001","32003-TXN002","35000-FLN001"],"networkTier":"","spciality":"Counseling/Social Work","city":"Austin","state":"TX","providerType":"DOCTOR","networkTierList":null,"networkIdList":["33000-TXN001","87200-TXN001","90006-TXN001","30003-TXN001","30003-TXN002","35000-FLN001"]}],"eligLeadId":null,"ssapApplicationId":null,"consumerData":null}

要屏蔽的数据

"dob":"1/1/2001"
"name":"Dr. Rakshit Kumar"
"smoker":"N"
"dentalEligible":"NO"

应该看起来像

"dob":"xxxxx"
"name":"xxxxx"
"smoker":"x"
"dentalEligible":"x"

【问题讨论】:

  • “需要脚本” - 然后聘请开发人员。 Stack Overflow 用户不是免费为您工作的。
  • 我不需要逐行完整的脚本。你应该能从我的标题中理解这一点。我得到了一个方向,感谢 jm666。即使是关键命令也会有所帮助。

标签: linux shell


【解决方案1】:

您的日志包含有效的 JSON 字符串。所以你需要这样做:

  1. 阅读每一行
  2. 从行中提取 JSON
  3. 读取 JSON 并将其解析为某些内部数据结构
  4. 更改所需字段
  5. 将更改的数据结构导出为 JSON
  6. 将屏蔽日志写入新文件
  7. 完成
  8. 利润

编辑

也许它可以从bash 使用一些工具来完成,但我正在使用 Perl 语言来完成这些任务。从零开始教 perl 真的是离题了。

或者,尝试用谷歌搜索一些manipulating JSON from bash 或类似的。

为了获取 JSON 部分,您可以使用类似下一个

while read -r line
do
    part1=$(sed 's/\(.*IndividualPlanList : \).*/\1/' <<< "$line")
    json=$(sed 's/.*IndividualPlanList : //' <<< "$line")

    #do something with the JSON
    newjson=$(echo "$json")

    #write out the new line
    echo "$part1$newjson"

done < logfile.txt

【讨论】:

  • 非常感谢 jm666。解析 JSON 的一些关键命令会有所帮助,因为我没有太多动手操作 JSON。
  • @user3885546 添加了一些指南
猜你喜欢
  • 2018-07-01
  • 2019-12-19
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多