Gragging Non-Printable Cygwin Characters

The grepping non printable characters don't seem to work for carriage return (control key ^ M).

usr@R923047 ~ $ head -3 test.ctl row 1 row 2 row 3 usr@R923047 ~ $ head -3 test.ctl | cat -nv 1 row 1^M 2 row 2^M 3 row 3 usr@R923047 ~ $ head -3 test.ctl | grep '[^[:print:]]' usr@R923047 ~ $ head -3 test.ctl | grep '[[:cntrl:]]' usr@R923047 ~ 
+4
source share
1 answer

According to the grep man page, you can specify -U or --binary :

Treat the file as binary. By default, on MS-DOS and MS-Windows, grep guesses the file you enter by looking at the contents of the first 32 Kbytes read from the file. If grep decides the file is a text file, it removes the CR characters from the original contents of the file (to make regular expressions with ^ and $ work correctly). The -U instruction overrides this hunch, forcing all files to be read and transferred verbatim to the appropriate mechanism; if the file is a text file with CR / LF pairs at the end of each line, this will cause some regular expressions to fail. This option does not affect platforms other than MS-DOS and MS-Windows.

So:

 $ head -3 test.ctl row 1 row 2 row 3 $ head -3 test.ctl | cat -nv 1 row 1^M 2 row 2^M 3 row 3 $ head -3 test.ctl | grep '[^[:print:]]' $ head -3 test.ctl | grep '[[:cntrl:]]' $ head -3 test.ctl | grep -U '[^[:print:]]' row 1 row 2 $ head -3 test.ctl | grep -U '[[:cntrl:]]' row 1 row 2 
+5
source

Source: https://habr.com/ru/post/1402629/


All Articles