Python 根据标准错误输出重写配置文件
我是Linux新手,为一个类构建了Fedora20。我们使用默认的开箱即用配置安装了Tripwire,我想从安装中获取标准错误来修复配置文件 要收集错误,请执行以下操作:Python 根据标准错误输出重写配置文件,python,bash,sed,Python,Bash,Sed,我是Linux新手,为一个类构建了Fedora20。我们使用默认的开箱即用配置安装了Tripwire,我想从安装中获取标准错误来修复配置文件 要收集错误,请执行以下操作: tripwire -m i -c tw.cfg 2> errors 要清除错误文件以进行处理,请执行以下操作: cat errors.txt | grep "/" | cut -d " " -f 3 > fixederrors 这为我提供了一个非常干净的文件,每行有一个路径,即: /垃圾箱/灰烬 /垃圾箱/灰
tripwire -m i -c tw.cfg 2> errors
要清除错误文件以进行处理,请执行以下操作:
cat errors.txt | grep "/" | cut -d " " -f 3 > fixederrors
这为我提供了一个非常干净的文件,每行有一个路径,即:
- /垃圾箱/灰烬
- /垃圾箱/灰箱
- /root/.Xresources
sed 's/^/#/g' fixederrors > commentederrors
或者,我考虑比较配置文件和修复错误并创建第三个文件。是否有方法获取两个文件,比较它们,并删除重复的数据
感谢您的帮助。我试过bash和python,但我知道的还不够多,所以在这一次中失败了。同样,这是为了我的成长,而不是在生产环境中 我们假设您的输入文件名为
fixederrors
“clean”
此输入作为名为config.original的配置文件
use /bin/ash
use /bin/bash
do stuff with /bin/ash.static and friends
/root/.Xresources
do other stuff...
在bash中使用此脚本
#!/bin/bash
Input_Conf_File="config.original" # Original configuration file
Output_Conf_File="config.new" # New configuration file
Input_Error_File="fixederrors" # File of cleaned error
rm -f $Output_Conf_File # Let's we create a new file erasing old
while read -r line ; do # meanwhile there are line in config file
AddingChar="" # No Char to add #
while IFS= read -r line2 ; do # For each line of Error file,
# here you can add specific rules for your match e.g
# line2=${line2}" " # if it has always a space after...
[[ $line == *$line2* ]] && AddingChar="#" # If found --> Change prefix "#"
done < $Input_Error_File
echo "${AddingChar}${line}" >> $Output_Conf_File # Print in new file
done < $Input_Conf_File
cat $Output_Conf_File # You can avoid this it's only to check results
exit 0
注:
IFS=
删除读取中的尾随空格
- 明智地使用,因为例如,匹配的
/bin/ash
将捕捉到带有/bin/ash的行。所有内容
;因此,如果在您的配置输入文件中存在一行,那么它也将被注释为/bin/ash.dynamic。如果事先不了解配置文件,则无法设置特定规则,但您可以从这里开始设置
sed“whatever”修复错误>注释错误
为什么这会更改您的配置文件?它将根据您的修复错误文件工作,不是吗?肯特,您是对的。我最初重写了配置文件,但出于测试目的创建了一个新的配置文件。显示一些示例:(部分)配置文件,(部分)修复错误和预期结果。@zonmei我不知道你想做什么…很好,这正是我需要的
#!/bin/bash
Input_Conf_File="config.original" # Original configuration file
Output_Conf_File="config.new" # New configuration file
Input_Error_File="fixederrors" # File of cleaned error
rm -f $Output_Conf_File # Let's we create a new file erasing old
while read -r line ; do # meanwhile there are line in config file
AddingChar="" # No Char to add #
while IFS= read -r line2 ; do # For each line of Error file,
# here you can add specific rules for your match e.g
# line2=${line2}" " # if it has always a space after...
[[ $line == *$line2* ]] && AddingChar="#" # If found --> Change prefix "#"
done < $Input_Error_File
echo "${AddingChar}${line}" >> $Output_Conf_File # Print in new file
done < $Input_Conf_File
cat $Output_Conf_File # You can avoid this it's only to check results
exit 0
#use /bin/ash
use /bin/bash
#do stuff with /bin/ash.static and friends
#/root/.Xresources
do other stuff...