Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regex match end of line in Windows

I have a .txt file created in Windows and now should be edited in Linux. I want to match the end of a line with grep. Let's say the content of the line I am going to find is "foo bar" in file bar. Then I issue the command grep 'r$' bar, but no output yielded.

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.

How can I match the content without convert the original file?

like image 225
Summer_More_More_Tea Avatar asked Jun 09 '11 07:06

Summer_More_More_Tea


2 Answers

If your grep supports -P (perl-regexp), then to match a CRLF:

grep -P '\r$' file

or:

grep Ctrl+VCtrl+M file

(Ctrl+VCtrl+M will produce ^M)

like image 122
dogbane Avatar answered Sep 30 '22 14:09

dogbane


Use a pattern which matches both line ends: \r?\n

like image 24
Aaron Digulla Avatar answered Sep 30 '22 15:09

Aaron Digulla