Board logo

标题: [求助]findstr查找以[开头并且]结尾的行 [打印本页]

作者: joyoustar     时间: 2008-8-1 16:03    标题: [求助]findstr查找以[开头并且]结尾的行

如题,假设文本文档123.txt中有类似以下字符 yiqiweiaoyunjiayou [ [my title ] ]end title [] [abc] [12345] abc[12345] abc[12345]def 我想查找以[开头,并且以]结尾的行并显示 [] [abc] [12345] 代码findstr "^[ ]$"不对,应该如何修改?谢谢! [ Last edited by joyoustar on 2008-8-2 at 10:44 AM ]

作者: yishanju     时间: 2008-8-1 16:37
findstr "[^$]"

作者: s11ss     时间: 2008-8-1 16:49
findstr ^[ 123.txt|findstr ]$

作者: FQ     时间: 2008-8-1 17:13
findstr /r "^\[.*\]$" 123.txt
在FINDSTR命令中[]属于正则表达式的元字符范围,所以必须要用\来进行字符化

作者: joyoustar     时间: 2008-8-1 17:17
谢谢楼上。

作者: s11ss     时间: 2008-8-1 17:19
Originally posted by FQ at 2008-8-1 05:13 PM:
findstr /r "^\[.*\]$" 123.txt
在FINDSTR命令中[]属于正则表达式的元字符范围,所以必须要用\来进行字符化
如果最后一行就是[12345],而不是空行的话,将不会显示[12345]。

作者: joyoustar     时间: 2008-8-1 17:26
同意楼上

作者: joyoustar     时间: 2008-8-1 17:27
为何不能显示最后一行[12345]呢

作者: FQ     时间: 2008-8-1 17:28
你不要光说我的代码,你自己也看看你的代码在说吧 aaa[dddd]

作者: FQ     时间: 2008-8-1 17:29
只要你最后一行有回车换行符我的代码足够

作者: joyoustar     时间: 2008-8-1 17:39
Originally posted by FQ at 2008-8-1 05:29 PM: 只要你最后一行有回车换行符我的代码足够
谢谢,但总感觉哪里再修改那么一点点就完美了!确实有时最后没有换行符。 s11ss兄的代码没错,但能用类似"findstr "^\[ * \]$" 123.txt"的最好不过了。

作者: s11ss     时间: 2008-8-1 19:59
Originally posted by FQ at 2008-8-1 05:28 PM: 你不要光说我的代码,你自己也看看你的代码在说吧 aaa[dddd]
这行本来就不应当显示,不是以[打头的嘛~~~~

作者: FQ     时间: 2008-8-2 08:29
唉,关键的关键是他显示了啊

作者: joyoustar     时间: 2008-8-2 10:43
Originally posted by s11ss at 2008-8-1 04:49 PM:
findstr ^[ 123.txt|findstr ]$
确实有些不对,我开始还没注意到这点,如果[前面有字符的话还是显示了,与要求不符。

作者: quya     时间: 2008-8-2 10:48
Originally posted by joyoustar at 2008-8-1 05:39 PM: 谢谢,但总感觉哪里再修改那么一点点就完美了!确实有时最后没有换行符。 s11ss兄的代码没错,但能用类似"findstr "^\[ * \]$" 123.txt"的最好不过了。
没有回车行, 就强行加一个, 我有时就这样做的。 在处理 123.txt 之前,不管它最后一行有没有回车符。先执行以下代码: echo. >>123.txt

作者: s11ss     时间: 2008-8-2 13:18
Originally posted by FQ at 2008-8-2 08:29 AM: 唉,关键的关键是他显示了啊
晕,原来我测试findstr ^[ 123.txt|findstr ]$的时候,aaa[dddd]后多了个空格,所以它没显示。 今天整出来一这个,好像可以:
findstr /b [ 123.txt|findstr /e ]

作者: joyoustar     时间: 2008-8-2 14:31
Originally posted by s11ss at 2008-8-2 01:18 PM: 晕,原来我测试findstr ^[ 123.txt|findstr ]$的时候,aaa[dddd]后多了个空格,所以它没显示。 今天整出来一这个,好像可以:
findstr /b [ 123.txt|findstr /e ]
兄的这个可以,谢谢了。不过原来那个findstr ^[ 123.txt|findstr ]$本身有问题,不是aaa[dddd]后多了个空格的问题。

作者: s11ss     时间: 2008-8-2 15:53
Originally posted by joyoustar at 2008-8-2 02:31 PM: 不过原来那个findstr ^[ 123.txt|findstr ]$本身有问题,不是aaa[dddd]后多了个空格的问题。
我说的意思也就是原来那个findstr ^[ 123.txt|findstr ]$本身有问题...