批量 findstr 正则表达式从单行 grep 一个项目
如何使用 Microsoft findstr 从一行中“grep”一个项目?
我有一行这样的内容:
<config>test</config><item>some data</item><date>2007-11-02</date><datestart>2007-10-31</datestart><path>d:\test\test\test.txt</path>
现在,如果我想 grep
和 中的内容,我该怎么写那个正则表达式?
需要注意的一件事是,我必须将 findstr 命令放入 for 循环中,以便 grep 日期并将其存储在变量中。
示例:
for /f "Tokens=* Delims=" %%i in ('findstr /r /c:"regex" file.txt') do (
set date=%%i
set date=!date:-=!
)
另外,我不想依赖令牌,因为它们可能会有所不同。
How can I use Microsoft findstr to "grep" an item from a single line?
I've got a line that goes something like this:
<config>test</config><item>some data</item><date>2007-11-02</date><datestart>2007-10-31</datestart><path>d:\test\test\test.txt</path>
Now if I want to grep the content inside of <date>
and </date>
how would I write that regular expression?
One thing to note is that I will have to take the findstr command into a for-loop in order to grep the date and store it in a variable.
example:
for /f "Tokens=* Delims=" %%i in ('findstr /r /c:"regex" file.txt') do (
set date=%%i
set date=!date:-=!
)
Also, I don't want to rely on tokens as they may vary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以使用一个技巧来完成您想要的操作:读取变量中的行后,将
和更改为分隔符,即不在行中(例如,
$
);然后,只需使用for /F "tokens=2 delims=$"
即可获取其中的内容。We may use a trick to do what you want: after read the line in a variable, change
<date>
and</date>
by a delimiter character that is not in the line (for example,$
); then, just use afor /F "tokens=2 delims=$"
to get the contents inside.您可以使用正则表达式
"[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0 -9]"
这应该与您的日期匹配。You could use the regex
"[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]"
which should match your date.