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?
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
)
Use a pattern which matches both line ends: \r?\n
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With