2017-05-25 188 views
0

我有一个csv文件。该文件有一些异常,因为它包含一些未知字符。查找csv文件中是否存在空格

这些字符出现在流行编辑器的第1535行(下面附有图片)。这个linedoes的终端中的sed命令没有显示任何内容。

$ sed '1535!d' sample.csv 
"sample_id","sample_column_text_1","sample_"sample_id","sample_column_text_1","sample_column_text_2","sample_column_text_3" 

但是下面是各种编辑器中文件的快照。

崇高文本 enter image description here

纳米 enter image description here

enter image description here

目录具有包含此字符/链不同的CSV文件。

我需要编写一个bash脚本来确定具有这些字符的文件。我怎样才能做到这一点?

+2

抬起你可以用'grep的 '[^ [:打印:]]''查找包含非打印字符的文件或线路。 – user3429660

+0

此命令在我的终端上停顿......不能前进。这个命令是做什么的,它是如何工作的? –

回答

1

以下是从;

http://www.linuxquestions.org/questions/programming-9/how-to-check-for-null-characters-in-file-509377/

#!/usr/bin/perl -w 

use strict; 

my $null_found = 0; 

foreach my $file (@ARGV) { 
    if (! open(F, "<$file")) { 
     warn "couldn't open $file for reading: $!\n"; 
     next; 
    } 

    while(<F>) { 
     if (/\000/) { 
      print "detected NULL at line $. in file $file\n"; 
      $null_found = 1; 
      last; 
     } 
    } 
    close(F); 
} 

exit $null_found; 

如果它工作需要,可以将其保存到一个文件中,nullcheck.pl并使其可执行文件;

chmod +x nullcheck.pl 

似乎把文件名作为输入数组,但如果它在任何发现会失败,所以我每次只在一个通过。以下命令用于运行脚本。

for f in $(find . -type f -exec grep -Iq . {} \; -and -print) ; do perl ./nullcheck.pl $f || echo "$f has nulls"; done 

以上find命令从Linux command: How to 'find' only text files?

+0

优雅的解决方案! –

1

您可以尝试tr

grep '\000' filename to find if the files contain the \000 characters.

你可以用它来去除NULL,并使其成为非NULL文件: tr < file-with-nulls -d '\000' > file-without-nulls