Linux Tips and Tricks

Linux Tips and Tricks

view non-printable characters

December8

we can use cat command and od commands to see the non-printable characters.

Let me explain how to create some non printable characters.

1) open the console and type the below.
2) echo “Hello <ctrl + v> <ctrl +m> World” > myfile.txt

Note :– <ctrl+v> means press control key and v in the keyboard.

3) Once the file is created, use cat command to view the file contents

1
2
3
4
$ echo "Hello ^M World" > myfile.txt
 
$ cat myfile.txt
 World

4)As you notice, Hello word is disappeared.

5) use the -v option in the cat command.

1
2
$ cat -v myfile.txt
Hello ^M World

6) Now you can see the word “Hello” but after that you can see some special character (^M) which is not printed while using the cat command
7) now we can use od command to see the special characters.

1
2
3
$ od -c myfile.txt 
0000000   H   e   l   l   o      \r       W   o   r   l   d  \n
0000016

You can clearly see there is come special character \r present between the Hello and World.

Control-M is carriage return without linefeed.

How to remove the special characters ?

In this case, we can remove \r (which is clearly found by od -c command )

1
2
3
4
5
6
7
$ cat -v myfile.txt 
Hello ^M World
 
$ sed -i 's/\r//' myfile.txt 
 
$ cat -v myfile.txt 
Hello  World
posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories