Invisible characters and processing in Linux text

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.