You are not logged in.

#1 2021-08-04 22:38:11

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Grep binary ... need to strip illegal chars [SOLVED]

Some how a log file I want to grep I think contains some illegal characters which makes grep think it's a binary and thus I cannot grep it.  What is a good strategy to clean it to pure text?  Googling hasn't turned up anything that works.  I was thinking something in sed but again, found nothing that works.  Thanks.

grep -i banks /scratch/text.log
grep: /scratch/text.log: binary file matches

Last edited by graysky (2021-08-05 00:52:06)

Offline

#2 2021-08-04 22:45:34

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,648

Re: Grep binary ... need to strip illegal chars [SOLVED]

grep -a ?

man grep wrote:

-a, --text
              Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

Offline

#3 2021-08-04 22:47:28

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,500
Website

Re: Grep binary ... need to strip illegal chars [SOLVED]

graysky wrote:

grep think it's a binary and thus I cannot grep it.

Na. The problem is you cannot grok it.

grep -i -a banks /scratch/text.log

But if you ever really do need to clean out some characters, I'd use something like the following:

tr -d -c '\t-~' < dirty > clean
# or
tr -d -c '\000-\177' < dirty > clean

EDIT: too slow ... I was looking for a :asci: character class for tr, but it may not have one.

Last edited by Trilby (2021-08-04 22:51:05)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2021-08-05 00:01:28

edacval
Member
From: .LT
Registered: 2008-10-23
Posts: 98

Re: Grep binary ... need to strip illegal chars [SOLVED]

strings /path | grep 'pattern'

Offline

#5 2021-08-05 00:51:40

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Re: Grep binary ... need to strip illegal chars [SOLVED]

That -a switch is excellent, thanks.

Trilby - Your tr command fixed it, thanks to you as well.

Offline

Board footer

Powered by FluxBB