Invisible characters and processing in Linux text
My colleague provides an interface that I call through curl, write the returned results to the file, and then process the file with grep and sed. When opened in a browser or curl, it is normal and the returned results are
Inetis_p1, masterpool; inetis_p2, masterpool; inetis_q1, masterpool; inetis_q2, masterpool; inetis_t1, masterpool; inetis_t2, masterpool;
However, when you use grep and sed to process a file after writing it, it is always a binary file.
[Root @ bkjia script] # grep master/var/tmp/qiehuan145.txt
Match to the binary file/var/tmp/qiehuan145.txt
It's strange that this is what we found when we opened it with vim.
Inetis_p1, masterpool ^ @; inetis_p2, masterpool ^ @; inetis_q1, masterpool ^ @; inetis_q2, masterpool ^ @; inetis_t1, masterpool ^ @; inetis_t2, masterpool ^ @;
Contains many invisible characters ^ @
You can also see it using cat-.
[Root @ bkjia script] # cat-A/var/tmp/qiehuan145.txt
Inetis_p1, masterpool ^ @; inetis_p2, masterpool ^ @; inetis_q1, masterpool ^ @; inetis_q2, masterpool ^ @; inetis_t1, masterpool ^ @; inetis_t2, masterpool ^ @; $
Invisible characters like this can be output to a file using printf, for example:
Printf 'test \ 011 \ 011 \ 011I am testing \ 014 \ 010 \ 012 more testing '>/tmp/test.txt
If you use cat to view
[Root @ bkjia script] # cat/tmp/test.txt
Test I am testing
More testing [root @ bkjia script] #
If you use cat-A to view
[Root @ bkjia script] # cat-A/tmp/test.txt
Test ^ I ^ II am testing ^ L ^ H $
More testing [root @ bkjia script] #
Now let's talk about how I solve it.
Cat-A/var/tmp/qiehuan145.txt | sed's/[\ ^ @] // G'
Use cat-A to read data, and then use sed to replace the data. Note that it must be escaped.
Linux text processing tool grep command details
Linux grep command
12 practical examples of the grep command in Linux
Linux basic commands-grep
Powerful and Common commands in Linux: find and grep
Linux grep commands and Regular Expressions
This article permanently updates the link address: