How to check in linux shell a line wrapper already generated by python script

I am running a Python script that generates a string and then executes a shell script using that string. I want to check the encoding of this line using linux shell, but not writing this line to a file (disk operations are slow). Is it possible to check the encoding of a string in Linux (Ubuntu) using only RAM? Something like:

check-encoding "My random encoding string"

The Python script validation encoding is slow too.


source to share

1 answer

Try a file utility. You can pass any string as a file argument to a file using echo

to pass to a utility with a parameter -

(many commands use a hyphen (-) instead of a filename as an argument to indicate when input should come from stdin rather than a file):

:~  $ echo "test" | file -i -
/dev/stdin: text/plain; charset=us-ascii

:~  $ echo "" | file -i -
/dev/stdin: text/plain; charset=utf-8


with a pipe to sed message:

:~  $ echo "" | file -i - | sed 's/.*charset=\(.*\)/\1/'


or awk (you can mix it up of course):

:~  $ echo "" | file -i - | awk '{ print $3 }'


also you can use python chardet module. Chardet comes with a command line script that reports the encoding of one or more files. Just install it with:

pip install chardet


and use with channel from echo:

:~  $ echo "" | chardetect
<stdin>: utf-8 with confidence 0.938125




All Articles