简体   繁体   English

用于屏蔽日志文件中敏感数据的Unix脚本?

[英]Unix script for masking sensitive data in the log files?

I have to write a script which will mask the sensitive data in the log files. 我必须编写一个脚本来掩盖日志文件中的敏感数据。 I am confused how to implement this? 我很困惑如何实现这个? Which option will be best for doing the same: 哪个选项最适合做同样的事情:

  • Using AWK 使用AWK
  • Using SED 使用SED
  • Using SED,AWK 使用SED,AWK
  • Using PERL 使用PERL
  • Using simple file read and searching logic. 使用简单的文件读取和搜索逻辑。

If you have any suggestions then please share. 如果您有任何建议,请分享。

Input File:
Name  Jack
Add   New York
Phone 333-333-3434

Output File:
Name   Jack
Add    New York
Phone  XXX-XXX-XXXX

I tried this using awk: 我用awk尝试过这个:

cat $HOME_DIR/testdata.dat | awk 'BEGIN{ 
    i=1; 
    FS=" "; 
} 
{ 
    for (i = 1; i < NF; i++) { 
        fld = $(i); 
        if( fld == "PHONE") { 
            printf ("%s$%s", $(i),$(i+1)); 
        } 
        else if( fld == "PIN") { 
            printf ("%s$%s", $(i),$(i+1)); 
        } 
        else if( fld == "DOB") { 
            printf ("%s$%s", $(i),$(i+1)); 
        } else { 
            printf ("%s", $(i)); 
        } 
    } 
    printf ("\n"); 
} 
END{ 
    i=1 
}' > $HOME_DIR/testdataupd.dat

One way using awk . 使用awk一种方法。 When found words phone , dob or pin at the beginning of the line (ignoring case) substitute in second field all characters but - with X . 当在行的开头找到单词phonedobpin (忽略大小写)时,在第二个字段中替换所有字符,但是-使用X The print command is executed for every line. 每行执行print命令。

awk '
    BEGIN { 
        IGNORECASE = 1
    }
    $1 ~ /^(phone|dob|pin)$/ {
        gsub( /[^-]/, "X", $2 )
    }
    { print }
' $HOME_DIR/testdata.dat >$HOME_DIR/testdataupd.dat

Here is a 90% answer, does not format the Xs as you asked though. 这是一个90%的答案,不会像你问的那样格式化Xs。

sed -re 's/(Phone )(([0-9]+)-?)*/\1xxxxx/g'

for more fields 更多领域

sed -r -e 's/(Phone )(([0-9]+)-?)*/\1xxxxx/g' -e 's/regexp-to-search-for/replacement-pattern/g' …

note: you can replace / with and character you like as long as it is same all 3 times eg s~regexp~rep~g 注意:你可以替换/和你喜欢的角色,只要它是相同的所有3次,例如s~regexp~rep~g

This might work for you (GNU sed): 这可能适合你(GNU sed):

sed '/^Phone\|^DOB\|^Pin/!b;h;s/\S*\s*//;s/[^-]/X/g;H;x;s/\(\S*\)\n\(\S*\)/\2/' file

Explanation: 说明:

  • /^Phone\\|^DOB\\|^Pin/!b only process lines beginning Phone , DOB or Pin (add more here) /^Phone\\|^DOB\\|^Pin/!b只处理开始PhoneDOBPin处理行(在这里添加更多)
  • h copy pattern space (PS) to hold space (HS) ie make a copy of the current line. h复制模式空间(PS)以保持空间(HS)即复制当前行。
  • s/\\S*\\s*// delete the first first field and following white space. s/\\S*\\s*//删除第一个第一个字段并跟随空格。
  • s/[^-]/X/g replace all occurrences of - 's with X 's in the remaining field. s/[^-]/X/g用剩余字段中的X替换所有出现的-
  • H append a newline and then the PS to the HS. H附加换行符然后将PS附加到HS。
  • x swap HS with PS x与PS交换HS
  • s/\\(\\S*\\)\\n\\(\\S*\\)/\\2/ replace the original second field with the amended one. s/\\(\\S*\\)\\n\\(\\S*\\)/\\2/用修改后的第二个字段替换原来的第二个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于查看日志文件的脚本 - Script to review log files 检查旋转日志文件的Perl脚本 - Perl script that checks rotated log files 使用unix ksh shell脚本或perl脚本和触发perl脚本监视新文件的文件夹 - Monitor folder for new files using unix ksh shell script or perl script and trigger perl script 如何获取保存在Unix中两个不同文件中的时间数据差异? - How to get Difference in time data kept in two different files in Unix? 扫描日志文件以获取最近30分钟的数据 - Scanning the log files for last 30 minutes of data 如何让 Log4Perl 为每个脚本使用单独的文件? - How to get Log4Perl to use separate files for each script? 我将如何删除所有<script> tags (and everything in between) from multiple files using UNIX? - How would I remove all <script> tags (and everything in between) from multiple files using UNIX? 将unix组权限设置为我的perl脚本输出的文件和目录? - Set unix group permissions to my files and directories output from my perl script? 如何编写UNIX命令或脚本来删除当前目录下所有子文件夹中相同类型的文件? - How to write a unix command or script to remove files of the same type in all sub-folders under current directory? 使用unix脚本根据子节点块将大型XML拆分为较小的文件 - Split large XML into smaller files based on chunks of child nodes using unix script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM