Issue
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?
Solution
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
)
Answered By - dogbane Answer Checked By - Willingham (WPSolving Volunteer)