LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete ^O from a text file (https://www.linuxquestions.org/questions/programming-9/delete-%5Eo-from-a-text-file-604682/)

pwc101 12-05-2007 09:04 AM

Delete ^O from a text file
 
I have a text file which when displayed in less appears thus:
Code:

^O 3600^O 3603^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 0^O 0^O 0^O 0^O 0^O 0^O 0
^O 0^O 3^O 8^O 5^O 5^O 5^O 1^O 4
^O 6^O 5^O 0^O 4^O 5^O 3^O 7^O 3
^O 7^O 3^O 2^O 2^O 3^O 4^O 3^O 3
^O 3^O 5^O 4^O 7^O 6^O 4^O 6^O 6
^O 1^O 4^O 1^O 6^O 2^O 5^O 3^O 7
^O 3^O 2^O 0^O 5^O 1^O 5^O 5^O 5
^O 2^O 1^O 3^O 1^O 5^O 3^O 4^O 3
^O 4^O 3^O 1^O 4^O 5^O 3^O 3^O 3
^O 6^O 6^O 3^O 2^O 3^O 8^O 2^O 3
^O 0^O 2^O 3^O 3^O 2^O 4^O 1^O 1
^O 3^O 2^O 1^O 2^O 0^O 4^O 1^O 2

I'd like to delete the ^O part of it in such a manner as I can do this in a script to any number of files.

In vim, I can get rid of them using :%s/^O//g where ^O = ctrl-v + ctrl-o, but I can't get the same to work in sed. I know these are null characters of some sort, I'd just like to delete them, leaving only the numbers.

ghostdog74 12-05-2007 09:06 AM

may or may not work. If not, try to use ctrl-v,ctrl-o too.
Code:

sed 's/\^O//g' file

matthewg42 12-05-2007 10:25 AM

If they appear in inverse video in less, they are probably ASCII SI characters (decimal 15, octal 017).

You can verify this with od:
Code:

$ od -ta yourfile |head -n 1
0000000  si  sp  3  6  0  0  si  sp  3  6  0  3  si  sp  0  si

Here I can see that the test file I made has space, space, "3600", si, space, "3603", ...
If you can verify that the characters in your file are also ASCII SI characters, you can use this command to remove them:
Code:

sed 's/\o017//g' yourfile > yourfile.new
Note that the \o017 is: backslash, lower case letter o, zero, one seven.

pwc101 12-05-2007 11:02 AM

Thanks for the responses.

I feel like a bit of a fool: having said that ctrl-v + ctrl-o worked in vim, I didn't try it directly in the CLI or using those keystrokes in my script, but it seems that it works perfectly well.

In the end I went with tr:
Code:

tr -d "^O" < input_file > output_file
although this is part of a longer piped command.

I've been looking for a way to do this sort of this for a while, specifically the od -ta command, and that's going to be useful in future!

Thanks again.


All times are GMT -5. The time now is 09:57 PM.