![grep이 캐리지 리턴과 일치하지 않습니다.](https://linux55.com/image/68554/grep%EC%9D%B4%20%EC%BA%90%EB%A6%AC%EC%A7%80%20%EB%A6%AC%ED%84%B4%EA%B3%BC%20%EC%9D%BC%EC%B9%98%ED%95%98%EC%A7%80%20%EC%95%8A%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
나는 노력한다캐리지 리턴이 있는 줄 찾기, 하지만 예상한 결과를 얻지 못했습니다. 나는 그것을 다음과 같은 개념 증명으로 단순화했습니다.
$ uname -a
CYGWIN_NT-6.1 Aodh 2.0.4(0.287/5/3) 2015-06-09 12:22 x86_64 Cygwin
$ grep --version
grep (GNU grep) 2.21
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
$ od -c cr_poc.txt
0000000 h e l l o w o r l d ; \r \n \r \n
0000020
$ od -x cr_poc.txt
0000000 6568 6c6c 206f 6f77 6c72 3b64 0a0d 0a0d
0000020
$ grep $'\r' cr_poc.txt; echo $?
1
캐릭터를 찾기 위해 다양한 방법을 시도했지만 \r
아무 것도 작동하지 않았습니다.
이것은 확실히 문제의 일부일 수 있는 Cygwin에 있다는 점에 유의하십시오.
답변1
다양한 입력을 연구하면서 grep
라인 엔딩에 고유한 마법이 있는 것 같은 느낌이 들었습니다.
$ printf "foo\rbar\n" | grep -oz $'\r' | od -c
0000000 \r \n
0000002
$ printf "foo\rbar\r\n" | grep -oz $'\r' | od -c
0000000
$ printf "foo\rbar\r" | grep -oz $'\r' | od -c
0000000 \r \n \r \n
0000004
(이것은 -z
모든 것을 일치시키려는 나의 어리석은 시도였습니다 grep
.) 그래서 맨페이지를 검색하여 LF
다음과 같은 결과를 얻었습니다.
-U, --binary
Treat the file(s) as binary. By default, under MS-DOS and MS-
Windows, grep guesses the file type by looking at the contents
of the first 32KB read from the file. If grep decides the file
is a text file, it strips the CR characters from the original
file contents (to make regular expressions with ^ and $ work
correctly). Specifying -U overrules this guesswork, causing all
files to be read and passed to the matching mechanism verbatim;
if the file is a text file with CR/LF pairs at the end of each
line, this will cause some regular expressions to fail. This
option has no effect on platforms other than MS-DOS and MS-
Windows.