【发布时间】:2015-01-23 10:02:18
【问题描述】:
我已尝试扫描堆栈溢出中的其他帖子,但无法让我的代码正常工作,因此我发布了一个新问题。
以下是文件temp 的内容。
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/<env:Body><dp:response xmlns:dp="http://www.datapower.com/schemas/management"><dp:timestamp>2015-01-
22T13:38:04Z</dp:timestamp><dp:file name="temporary://test.txt">XJzLXJlc3VsdHMtYWN0aW9uX18i</dp:file><dp:file name="temporary://test1.txt">lc3VsdHMtYWN0aW9uX18i</dp:file></dp:response></env:Body></env:Envelope>
此文件包含两个文件名 test.txt 和 test1.txt 的 base64 编码内容。我想提取每个文件的base64编码内容,分别将文件test.txt和text1.txt分开。
为此,我必须删除 base64 内容周围的 xml 标签。我正在尝试以下命令来实现这一点。但是,它没有按预期工作。
sed -n '/test.txt"\>/,/\<\/dp:file\>/p' temp | perl -p -e 's@<dp:file name="temporary://test.txt">@@g'|perl -p -e 's@</dp:file>@@g' > test.txt
sed -n '/test1.txt"\>/,/\<\/dp:file\>/p' temp | perl -p -e 's@<dp:file name="temporary://test1.txt">@@g'|perl -p -e 's@</dp:file></dp:response></env:Body></env:Envelope>@@g' > test1.txt
下面的命令:
sed -n '/test.txt"\>/,/\<\/dp:file\>/p' temp | perl -p -e 's@<dp:file name="temporary://test.txt">@@g'|perl -p -e 's@</dp:file>@@g'
产生输出:
XJzLXJlc3VsdHMtYWN0aW9uX18i
<dp:file name="temporary://test1.txt">lc3VsdHMtYWN0aW9uX18i</dp:response> </env:Body></env:Envelope>`
然而,在输出中我只期待第一行XJzLXJlc3VsdHMtYWN0aW9uX18i。我在哪里犯错了?
当我在命令下运行时,我得到了预期的输出:
sed -n '/test1.txt"\>/,/\<\/dp:file\>/p' temp | perl -p -e 's@<dp:file name="temporary://test1.txt">@@g'|perl -p -e 's@</dp:file></dp:response></env:Body></env:Envelope>@@g'
它产生下面的字符串
lc3VsdHMtYWN0aW9uX18i
然后我可以轻松地将其路由到 test1.txt 文件。
更新
我已通过更新源文件内容来编辑问题。源文件不包含任何换行符。在这种情况下,当前的解决方案将不起作用,我已经尝试过但失败了。 wc -l temp 必须输出到1。
OS: solaris 10
Shell: bash
【问题讨论】:
-
所以你也不想要这个
lc3VsdHMtYWN0aW9uX18i? -
是的,除了
XJzLXJlc3VsdHMtYWN0aW9uX18i,我什么都不想要 -
这应该可以工作
awk 'match($0,/dp:file name="([^"]+)">([^<]+)</,a){print a[1] > a[2]}' file -
我更新了我的问题,让我的要求更清晰
-
awk: syntax error near line 1 awk: bailing out near line 1如果我使用您的代码,则会出现上述错误。