[英]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: 哪个选项最适合做同样的事情:
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
. 当在行的开头找到单词phone
, dob
或pin
(忽略大小写)时,在第二个字段中替换所有字符,但是-
使用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
只处理开始Phone
, DOB
或Pin
处理行(在这里添加更多) 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.