使用源文件中的数据从XML文件中获取块
我修改了这个问题,因为我读了一点XML 我有一个包含authNumber列表的文件源文件。使用源文件中的数据从XML文件中获取块,xml,awk,sed,grep,pcregrep,Xml,Awk,Sed,Grep,Pcregrep,我修改了这个问题,因为我读了一点XML 我有一个包含authNumber列表的文件源文件。 111222 111333 111444 等等。 我需要搜索该列表中的数字,并在相应的XML文件中找到它们。 在xml文件中,行的格式如下: 111222 这可以通过使用grep轻松实现,但是我需要包含事务的整个块 该块以以下内容开始: 或和/或其他一些变体。事实上,如果这样做是可能的,那就最好了 该块以 它不需要优雅或高效。我只是需要它来工作。我怀疑一些交易正在退出,我需要一种快速的方法来检查那些未被
111222
111333
111444
等等。
我需要搜索该列表中的数字,并在相应的XML文件中找到它们。
在xml文件中,行的格式如下:
111222
这可以通过使用grep轻松实现,但是我需要包含事务的整个块
该块以以下内容开始:
或
和/或其他一些变体。事实上,
如果这样做是可能的,那就最好了
该块以
它不需要优雅或高效。我只是需要它来工作。我怀疑一些交易正在退出,我需要一种快速的方法来检查那些未被处理的交易
如果有帮助的话,这里有一个指向原始(已消毒)xml的链接
我想摘录的是:
每个结果的大小会有所不同,因为根据购买的产品数量,每笔交易的长度可能会有很大差异。在xml结果中,您可以看到我根据trpcAuthCode列表11122111333111444提取了所需的xml。关于xml和awk问题,您经常会发现专家(如果他们的声誉是k)的评论,awk中的xml处理很复杂或不够。据我所知,脚本是出于个人和/或调试目的而需要的。对于这一点,我的解决方案应该足够了,但请记住,它对任何合法的XML文件都不起作用 根据您的描述,脚本的草图为:
匹配,则开始录制
,则获取其内容并与列表进行比较。在匹配的情况下,记住输出块
匹配,停止录制。如果已启用输出,则打印记录的块,否则丢弃它BEGIN {
record = 0 # state for recording
buffer = "" # buffer for recording
found = 0 # state for found auth code
# build temp. array from authCodes which has to be pre-defined
split(authCodes, list, "\n")
# build final array where values become keys
for (i in list) authCodeList[list[i]]
# for debugging: output of authCodeList
print "<!-- authCodeList:"
for (authCode in authCodeList) {
print authCode
}
print "-->"
}
/<trans( [^>]*)?>/ {
record = 1 # start recording
buffer = "" # clear buffer
found = 0 # reset state for found auth code
}
record {
buffer = buffer"\n"$0 # record line (if recording is enabled)
}
record && /<trpcAuthCode>/ {
# extract auth code
authCode = gensub(/^.*>([^<]*)<\/trpcAuthCode.*$/, "\\1", "g")
# check whether auth code in authCodeList
found = authCode in authCodeList
}
/<\/trans>/ {
record = 0 # stop recording
# print buffer if auth code has been found
if (found) {
print buffer
}
}
我针对您的示例文件测试了脚本(在Windows10上使用cygwin中的bash),得到了四个匹配的块。我有点担心输出,因为在示例输出中只有三个匹配块。但是用视觉检查我的输出似乎是合适的。(所有四次点击都包含一个匹配的
元素。)
为了演示,我稍微减少了示例输入sample.xml
:
<?xml version="1.0"?>
<transSet periodID="1" periodname="Shift" longId="2017-04-27" shortId="052" site="12345">
<trans type="periodClose">
<trHeader>
</trHeader>
</trans>
<printCashier>
<cashier sysid="7" empNum="07" posNum="101" period="11">A.Dude</cashier>
</printCashier>
<trans type="printCashier">
<trHeader>
<cashier sysid="7" empNum="07" posNum="101" period="11">A.Dude</cashier>
<posNum>101</posNum>
</trHeader>
</trans>
<trans type="journal">
<trHeader>
</trHeader>
</trans>
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpCardInfo>
<trpcAccount>1234567890123456</trpcAccount>
<trpcAuthCode>532524</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpPaycode mop="3" cat="1" nacstendercode="generic" nacstendersubcode="generic">CREDIT</trpPaycode>
<trpAmt>61.77</trpAmt>
<trpCardInfo>
<trpcAccount>2345678901234567</trpcAccount>
<trpcAuthCode>111222</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
<trans type="periodClose">
<trHeader>
<date>2017-04-27T23:50:17-04:00</date>
</trHeader>
</trans>
<endTotals>
<insideSales>445938.63</insideSales>
</endTotals>
</transSet>
在示例会话中使用两个输入文件:
$ ./filter-xml-trpcAuthCode.sh
ERROR: Illegal number of command line arguments!
Usage:
filter-xml-trpcAuthCode.sh XML_FILE AUTH_CODES
$ ./filter-xml-trpcAuthCode.sh sample.xml authCodes.txt
<!-- authCodeList:
111222
111333
111444
-->
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpPaycode mop="3" cat="1" nacstendercode="generic" nacstendersubcode="generic">CREDIT</trpPaycode>
<trpAmt>61.77</trpAmt>
<trpCardInfo>
<trpcAccount>2345678901234567</trpcAccount>
<trpcAuthCode>111222</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
$ ./filter-xml-trpcAuthCode.sh main.xml authCodes.txt >output.txt
$
$。/filter-xml-trpcAuthCode.sh
错误:命令行参数的数量非法!
用法:
filter-xml-trpcAuthCode.sh xml_文件验证代码
$./filter-xml-trpcAuthCode.sh sample.xml authCodes.txt
31054
信用
61.77
2345678901234567
111222
$./filter-xml-trpcAuthCode.sh main.xml authCodes.txt>output.txt
$
最后一个命令将输出重新定向到一个文件
output.txt
,以后可能会对该文件进行检查或处理。关于XML和awk问题,您经常会发现专家们(如果他们的声誉是k)的评论,即awk中的XML处理很复杂或不够。据我所知,脚本是出于个人和/或调试目的而需要的。对于这一点,我的解决方案应该足够了,但请记住,它对任何合法的XML文件都不起作用
根据您的描述,脚本的草图为:
匹配,则开始录制
,则获取其内容并与列表进行比较。在匹配的情况下,记住输出块
匹配,停止录制。如果已启用输出,则打印记录的块,否则丢弃它BEGIN {
record = 0 # state for recording
buffer = "" # buffer for recording
found = 0 # state for found auth code
# build temp. array from authCodes which has to be pre-defined
split(authCodes, list, "\n")
# build final array where values become keys
for (i in list) authCodeList[list[i]]
# for debugging: output of authCodeList
print "<!-- authCodeList:"
for (authCode in authCodeList) {
print authCode
}
print "-->"
}
/<trans( [^>]*)?>/ {
record = 1 # start recording
buffer = "" # clear buffer
found = 0 # reset state for found auth code
}
record {
buffer = buffer"\n"$0 # record line (if recording is enabled)
}
record && /<trpcAuthCode>/ {
# extract auth code
authCode = gensub(/^.*>([^<]*)<\/trpcAuthCode.*$/, "\\1", "g")
# check whether auth code in authCodeList
found = authCode in authCodeList
}
/<\/trans>/ {
record = 0 # stop recording
# print buffer if auth code has been found
if (found) {
print buffer
}
}
我针对您的示例文件测试了脚本(在Windows10上使用cygwin中的bash),得到了四个匹配的块。我有点担心输出,因为在示例输出中只有三个匹配块。但是用视觉检查我的输出似乎是合适的。(所有四次点击都包含一个匹配的
元素。)
为了演示,我稍微减少了示例输入sample.xml
:
<?xml version="1.0"?>
<transSet periodID="1" periodname="Shift" longId="2017-04-27" shortId="052" site="12345">
<trans type="periodClose">
<trHeader>
</trHeader>
</trans>
<printCashier>
<cashier sysid="7" empNum="07" posNum="101" period="11">A.Dude</cashier>
</printCashier>
<trans type="printCashier">
<trHeader>
<cashier sysid="7" empNum="07" posNum="101" period="11">A.Dude</cashier>
<posNum>101</posNum>
</trHeader>
</trans>
<trans type="journal">
<trHeader>
</trHeader>
</trans>
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpCardInfo>
<trpcAccount>1234567890123456</trpcAccount>
<trpcAuthCode>532524</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpPaycode mop="3" cat="1" nacstendercode="generic" nacstendersubcode="generic">CREDIT</trpPaycode>
<trpAmt>61.77</trpAmt>
<trpCardInfo>
<trpcAccount>2345678901234567</trpcAccount>
<trpcAuthCode>111222</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
<trans type="periodClose">
<trHeader>
<date>2017-04-27T23:50:17-04:00</date>
</trHeader>
</trans>
<endTotals>
<insideSales>445938.63</insideSales>
</endTotals>
</transSet>
在示例会话中使用两个输入文件:
$ ./filter-xml-trpcAuthCode.sh
ERROR: Illegal number of command line arguments!
Usage:
filter-xml-trpcAuthCode.sh XML_FILE AUTH_CODES
$ ./filter-xml-trpcAuthCode.sh sample.xml authCodes.txt
<!-- authCodeList:
111222
111333
111444
-->
<trans type="network sale" recalled="false">
<trHeader>
<termMsgSN type="FINANCIAL" term="908">31054</termMsgSN>
</trHeader>
<trPaylines>
<trPayline type="sale" sysid="1" locale="DOLLAR">
<trpPaycode mop="3" cat="1" nacstendercode="generic" nacstendersubcode="generic">CREDIT</trpPaycode>
<trpAmt>61.77</trpAmt>
<trpCardInfo>
<trpcAccount>2345678901234567</trpcAccount>
<trpcAuthCode>111222</trpcAuthCode>
</trpCardInfo>
</trPayline>
</trPaylines>
</trans>
$ ./filter-xml-trpcAuthCode.sh main.xml authCodes.txt >output.txt
$
$。/filter-xml-trpcAuthCode.sh
错误:命令行参数的数量非法!
用法:
filter-xml-trpcAuthCode.sh xml_文件验证代码
$./filter-xml-trpcAuthCode.sh sample.xml authCodes.txt
31054
信用
61.77
2345678901234567
111222
$./filter-xml-trpcAuthCode.sh main.xml authCodes.txt>output.txt
$
最后一个命令将输出重新定向到一个文件
output.txt
,以后可能会对其进行检查或处理。显示输入xml和预期结果托克,我们得到了输入,它仍将显示预期结果我正在为此绞尽脑汁。没有人能提供帮助吗?如果没有预期的结果,您将无法快速获得帮助。如果有帮助的话,我已经包括了源和结果链接。显示输入xml和预期结果。我们得到了输入,它仍然是