Tim*_*ell 115 linux bash grep newline
我想在Linux上用grep搜索包含dos行结尾的文件.像这样的东西:
grep -IUr --color '\r\n' .
Run Code Online (Sandbox Code Playgroud)
以上似乎与文字rn相匹配,这不是所期望的.
这个输出将通过xargs传输到todos,将crlf转换为lf,就像这样
grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'
Run Code Online (Sandbox Code Playgroud)
Tho*_*mee 146
grep可能不是你想要的工具.它将为每个文件中的每个匹配行打印一行.除非你想在10行文件上运行待机10次,否则grep并不是最好的方法.使用find在树中的每个文件上运行文件,然后通过"CRLF"的那个文件,将为每个具有dos样式行结尾的文件获取一行输出:
find . -not -type d -exec file "{}" ";" | grep CRLFRun Code Online (Sandbox Code Playgroud)
会得到你这样的东西:
./1/dos1.txt: ASCII text, with CRLF line terminators
./2/dos2.txt: ASCII text, with CRLF line terminators
./dos.txt: ASCII text, with CRLF line terminatorsRun Code Online (Sandbox Code Playgroud)
pjz*_*pjz 112
使用Ctrl+ V,Ctrl+ M在grep字符串中输入文字回车符.所以:
grep -IUr --color "^M"
Run Code Online (Sandbox Code Playgroud)
将起作用 - 如果^M您按照我的建议输入了文字CR.
如果需要文件列表,还要添加该-l选项.
说明
-I 忽略二进制文件-U阻止grep去除CR字符.默认情况下,如果它确定它是文本文件,它会这样做.-r 以递归方式读取每个目录下的所有文件. 小智 51
grep -IUlr $'\r'
Run Code Online (Sandbox Code Playgroud)
Lin*_*lin 16
如果您的grep版本支持-P(--perl-regexp)选项,那么
grep -lUP '\r$'
Run Code Online (Sandbox Code Playgroud)
可用于.
小智 7
# list files containing dos line endings (CRLF)
cr="$(printf "\r")" # alternative to ctrl-V ctrl-M
grep -Ilsr "${cr}$" .
grep -Ilsr $'\r$' . # yet another & even shorter alternative
Run Code Online (Sandbox Code Playgroud)
dos2unix有一个文件信息选项,可用于显示要转换的文件:
dos2unix -ic /path/to/file\nRun Code Online (Sandbox Code Playgroud)\n要递归地执行此操作,您可以使用bash\xe2\x80\x99sglobstar选项,该选项对于当前 shell 启用shopt -s globstar:
dos2unix -ic ** # all files recursively\ndos2unix -ic **/file # files called \xe2\x80\x9cfile\xe2\x80\x9d recursively\nRun Code Online (Sandbox Code Playgroud)\n或者,您可以使用find以下方法:
find -type f -exec dos2unix -ic {} + # all files recursively (ignoring directories)\nfind -name file -exec dos2unix -ic {} + # files called \xe2\x80\x9cfile\xe2\x80\x9d recursively\nRun Code Online (Sandbox Code Playgroud)\n