To find a file with a line feed code CR + LF under the current directory on Linux or Cygwin, do the following.
This is the method using grep. You have specified -l to display only the file name in grep. (This method will include the binary file in the result, but it may be more meaningful search if you specify the extension as in Example 2).
Addendum (2014/06/23 19:10): It was intended to detect CR + LF, but
Currently, the following commands will hit even if only CR is used. If you find a good way, fix it. </ del> I found a good way to fix it.
Addendum (2014/06/24 15:25): I was able to detect CR + LF by the method found by elfmimi.
Example 1
$ find . -type f | xargs grep -lzUP '\r\n'
Example 2
$ find . -name "*.java" | xargs grep -lzUP '\r\n'
For the time being, I made my own command in C language and dealt with it (^ ー ^);
(This way </ del> also includes binary files in the result)
How to use
$ gcc -o /usr/local/bin/crlf crlf.c
$ find . -type f | xargs crlf
crlf.c
#include <stdio.h>
int main(int argc, char **argv)
{
if(argc < 2) return 1;
int i;
for(i=1; i<argc; i++)
{
FILE *fp = fopen(argv[i], "r");
if(!fp) continue;
int last = 0;
int c = 0;
while((c=fgetc(fp)) != EOF)
{
if(last=='\r' && c=='\n')
{
printf("%s\n", argv[i]);
break;
}
last=c;
}
fclose(fp);
}
return 0;
}
The method is to take out the output (standard output) when the file command of Linux (or Cygwin) is executed, and output the file name if "with CRLF line terminators" is included. Binary files can be excluded. (Before doing it in the shell, I tried to realize it using C language commands for the time being)
How to use
$ gcc -o /usr/local/bin/textcrlf textcrlf.c
$ find . -type f | xargs textcrlf
textcrlf.c
#include <stdio.h>
#include <string.h>
#define MAXLEN (1024)
int main(int argc, char **argv)
{
if(argc < 2) return 1;
int i;
for(i=1; i<argc; i++)
{
static char cmd[MAXLEN];
sprintf(cmd, "file %s", argv[i]);
FILE *fpin = popen(cmd, "r");
if(!fpin) continue;
static char line[MAXLEN];
if(!fgets(line, MAXLEN, fpin))
{
pclose(fpin);
continue;
}
if(strstr(line, "with CRLF line terminators"))
{
printf("%s\n", argv[i]);
}
pclose(fpin);
}
return 0;
}
I also made a C command to find a file with a line feed code of LF on Linux (or Cygwin). This command also depends on the output of the file command.
How to use
$ gcc -o /usr/local/bin/textlf textlf.c
$ find . -type f | xargs textlf
textlf.c
#include <stdio.h>
#include <string.h>
#define MAXLEN (1024)
int main(int argc, char **argv)
{
if(argc < 2) return 1;
int i;
for(i=1; i<argc; i++)
{
static char cmd[MAXLEN];
sprintf(cmd, "file %s", argv[i]);
FILE *fpin = popen(cmd, "r");
if(!fpin) continue;
static char line[MAXLEN];
if(!fgets(line, MAXLEN, fpin))
{
pclose(fpin);
continue;
}
if(strstr(line, "with CR line terminators"))
{
;
}
else if(strstr(line, "with CRLF line terminators"))
{
;
}
else if(strstr(line, "ASCII text"))
{
printf("%s\n", argv[i]);
}
pclose(fpin);
}
return 0;
}
Recommended Posts