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中创建了一个.txt文件,现在应该在Linux中进行编辑。我想匹配行尾与grep,假设我要查找的行内容是文件栏中的foo bar。然后我发出grep 'r$' bar命令,没有输出。
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.
在Windows中,新的一行由'\r\n'组成,与Linux/Unix不同,只有一个'\n',我认为肯定有一些微妙的东西与此相关。然后我用dos2unix对文件进行转换,这样就可以了。
My question is how can I match the content without convert the original file?
我的问题是如何在不转换原始文件的情况下匹配内容?
Any advice will be highly appreciated.
如有任何建议,我们将不胜感激。
Thanks and Best Regards.
谢谢和最好的祝福。
3 个解决方案
#1
7
If your grep supports -P
(perl-regexp), then to match a CRLF:
如果您的grep支持-P (perl-regexp),则匹配CRLF:
grep -P '\r$' file
or:
或者:
grep Ctrl+VCtrl+M file
grep Ctrl + VCtrl + M文件
(Ctrl+VCtrl+M will produce ^M
)
(Ctrl + VCtrl + M将产生^米)
#2
6
Use a pattern with matches both line ends: r\r?\n
使用模式与匹配的两条线结束:r\r?\n
#3
-1
give it a try awk
试试awk
awk '/r$/' RS='\r\n' bar
or
或
awk '/r\r$/' bar
#1
7
If your grep supports -P
(perl-regexp), then to match a CRLF:
如果您的grep支持-P (perl-regexp),则匹配CRLF:
grep -P '\r$' file
or:
或者:
grep Ctrl+VCtrl+M file
grep Ctrl + VCtrl + M文件
(Ctrl+VCtrl+M will produce ^M
)
(Ctrl + VCtrl + M将产生^米)
#2
6
Use a pattern with matches both line ends: r\r?\n
使用模式与匹配的两条线结束:r\r?\n
#3
-1
give it a try awk
试试awk
awk '/r$/' RS='\r\n' bar
or
或
awk '/r\r$/' bar