【发布时间】:2016-12-21 20:31:03
【问题描述】:
我想搜索 (awk/grep/sed) 到几个 XML 文件 (pom.xml 文件) 中跳过一些文件夹。此外,第一个条件是它们必须包含标签<module>。对于这些情况,我想打印出那些不包含以下确切序列的内容(它是自动生成的代码 - 它将帮助我检测是否有人修改了该序列):
<!--
| Start of user code (user defined modules)
|-->
<!--
| End of user code
|-->
我被困在这里:
fileArray=($(find . -type f -not -path "./folder1/*" -not -path "*/folder2/*" -not -path "./folder3/*" -name "pom.xml" \
| xargs awk -v RS='^$' 'match($0,/\<module>[^\n]+/,a){print a[0]}'))
请给点建议?
---更新:
#!/bin/sh
###########################################################
# Checks for "user code" <modules> defined in pom files.
###########################################################
function check()
{
# http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
OLDIFS=$IFS
IFS=$'\n'
# Read all pom files into an array
# - Search for user code modules: It searches for the tag <module> into the pom files and in case they contain modules,
#checks if the autogenerated section has been modified. Reading text secuence from foo.txt file
#
# - Exclude model folder as the codegen poms therein require such a repository
fileArray=($(find . -type f -not -path "./folder1/*" -not -path "*/folder2/*" -not -path "./folder3/*" -name "pom.xml" \
| xargs `awk -v RS='^$' 'NR==FNR{str=$0;next} /<module>/ && !index($0,str){print FILENAME}' sequence {} +`))
IFS=$OLDIFS
# get length of an array
numberOfFiles=${#fileArray[@]}
# read all filenames
for (( i=0; i<${numberOfFiles}; i++ ));
do
echo "ERROR:Found user code modules (file:line:occurrence): ${fileArray[$i]}"
done
if [ "$numberOfFiles" != "0" ]; then
echo "SUMMARY:Found $numberOfFiles pom.xml file(s) containing user code modules."
exit 1
fi
}
check
----UPDATE(最后一个控制台输出)
:~/temp> bash script.sh
awk: cmd. line:1: fatal: cannot open file `{}' for reading (No such file or directory)
ERROR:Found user code modules (file:line:occurrence): ./test_folder/test4/pom.xml ./tes t_folder/test1/pom.xml ./test_folder/test2/pom.xml ./test_folder/test3/pom.xml
SUMMARY:Found 1 pom.xml file(s) containing user code modules.
【问题讨论】:
-
我建议使用 XML/HTML 解析器 (xmllint, xmlstarlet ...)。
-
从一个文件的脚本开始(没有 awk/find)。
-
我亲眼目睹的最严重的数据丢失事件是由假设(关键计费)日志遵循特定命名约定的备份维护代码引起的。缓冲区溢出将垃圾转储到文件名中,垃圾包括被空格包围的
*,脚本删除了目录中的每个日志文件。如果您只编写代码来处理您认为可能发生的情况,那么您就是在您认为不可能发生的地方编写错误。 -
...如果您在认为脚本的正确性不重要的情况下粗心大意,您真的认为您能够突然遵循良好的习惯和实践吗?一年中的某一天,您在做真正重要的事情,而没有养成在其余时间关注稳健实践的习惯?
-
在上述数据丢失的情况下也非常严格——
[0-9a-f]{24}几乎和他们来的时候一样严格。顺便说一句,您正在搜索评论而不是语义数据这一事实至关重要——它有助于将您与“仅使用 XMLStarlet / xmllint”的答案隔离开来——因此我已将其修改为标题。跨度>