如何在Windows中使用正则表达式匹配行尾? [英] How to use regex match end of line in Windows?
问题描述
我在Windows中创建了一个.txt文件,现在应该在Linux中对其进行编辑.我想用grep匹配一行的结尾,比方说我要找到的行的内容是文件bar
中的"foo bar".然后我发出命令grep 'r$' bar
,没有输出.
I have a .txt file created in Windows and now should be edited in Linux. I want to match end of a line with grep, let's say content of the line I gonna to find is "foo bar" in file bar
. Then I issue the command grep 'r$' bar
, no output yielded.
鉴于Windows中有一个新行由'\ r \ n'组成,与Linux/Unix中的一个单独的'\ n'不同,我认为一定与此有些微妙的关系.然后我用dos2unix
和voila转换文件,就可以了.
Given in Windows a new line consists of '\r\n', different from Linux/Unix a single '\n', I think there must be something subtle related to this. Then I convert the file with dos2unix
and voila, it works.
我的问题是如何在不转换原始文件的情况下匹配内容?
My question is how can I match the content without convert the original file?
任何建议将不胜感激.
谢谢和最好的问候.
推荐答案
如果您的grep支持-P
(perl-regexp),则要匹配CRLF:
If your grep supports -P
(perl-regexp), then to match a CRLF:
grep -P '\r$' file
或:
grep Ctrl + V Ctrl + M 文件
( Ctrl + V Ctrl + M 将产生^M
)
这篇关于如何在Windows中使用正则表达式匹配行尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!