문제
나는 유닉스 라인 엔딩을 가지고 있어야하는 파일 (수백)의 파일이 있습니다. 나는 그들 중 일부가 Windows 라인 엔딩을 가지고 있다고 강력하게 의심하며, 어떤 것이 어떤 일을하는지 프로그래밍 방식으로 알아 내고 싶습니다.
난 그냥 달릴 수 있다는 걸 알아
flip -u또는 모든 것을 변환하기 위해 스크립트에서 비슷한 것이지만 먼저 변경 해야하는 파일을 식별 할 수 있기를 원합니다.
해결책
Grep을 사용할 수 있습니다
egrep -l $'\r'\$ *
다른 팁
당신은 사용할 수 있습니다 file
도구, 라인 엔딩 유형을 알려줍니다. 아니면 그냥 사용할 수 있습니다 dos2unix -U
시작한 내용에 관계없이 모든 것을 유닉스 라인 엔딩으로 변환합니다.
라인을 따라 무언가 :
perl -p -e 's[\r\n][WIN\n]; s[(?<!WIN)\n][UNIX\n]; s[\r][MAC\n];' FILENAME
그 regexp의 일부는 정제 및 정리가 필요할 수 있지만.
그러면 각 줄의 끝에서 WIN, MAC 또는 UNIX로 파일을 출력합니다. 파일이 어떻게 든 끔찍한 혼란 (또는 차이)이고 결말이 혼합 된 경우 좋습니다.
가장 실패한 대답은 다음과 같습니다. Stimms Answer는 하위 디렉토리 및 이진 파일을 설명하지 않습니다
find . -type f -exec file {} \; | grep "CRLF" | awk -F ':' '{ print $1 }'
- 사용
file
파일 유형을 찾으려면. CRLF가있는 사람에게는 Windows Return 문자가 있습니다. 출력file
a:
, 첫 번째 필드는 파일의 경로입니다.
Unix uses one byte, 0x0A (LineFeed), while windows uses two bytes, 0x0D 0x0A (Carriage Return, Line feed).
If you never see a 0x0D, then it's very likely Unix. If you see 0x0D 0x0A pairs then it's very likely MSDOS.
Windows use char 13 & 10 for line ending, unix only one of them ( i don't rememeber which one ). So you can replace char 13 & 10 for char 13 or 10 ( the one, which use unix ).
When you know which files has Windows line endings (0x0D 0x0A
or \r \n
), what you will do with that files? I supose, you will convert them into Unix line ends (0x0A
or \n
). You can convert file with Windows line endings into Unix line endings with sed
utility, just use command:
$> sed -i 's/\r//' my_file_with_win_line_endings.txt
You can put it into script like this:
#!/bin/bash
function travers()
{
for file in $(ls); do
if [ -f "${file}" ]; then
sed -i 's/\r//' "${file}"
elif [ -d "${file}" ]; then
cd "${file}"
travers
cd ..
fi
done
}
travers
If you run it from your root dir with files, at end you will be sure all files are with Unix line endings.