Search code examples
bashmacosgrepbsd

Counting lines with non-printable characters with BSD


I am trying to sort out some bad data in a file on a BSD-style system, which means that I do not have the -P option in grep. I have 7 million lines of data, and a subset has some strange characters. If you to a "less" on the file, you'll see something like this:

290437430@89
9^@0333465@88
290348389@87
290342818@8^@

The ^@ is from a bad character that is not ASCII that showed up due to noise on the serial line when the characters were sent. These lines are corrupt, and I want to count the number of corrupt data strings.

Any suggestions would be greatly appreciated.


Solution

  • As per Chepner's suggestion adding following solution here:

    grep -c '\x00' Input_file
    

    Following 2 will give only literal characters only.

    If you want to only count @ then a simple grep could help you on same.

    grep -c "@"  Input_file
    

    Or in case of counting ^@ then following may help you on same.

    grep -c "\^@"  Input_file