1

How can I remove each null character from a text file and replace it with a blank space ?

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
  • Or run the `dos2unix` conversion? – David C. Rankin Apr 21 '15 at 09:05
  • I need to be able to do it from git-bash (msysgit) or cygwin. And I prefer to not install any additional applications. – Svetlin Zarev Apr 21 '15 at 09:11
  • Then a `sed` type solution will work. I haven't check on what the `null` match is, whether `\0` or `\000` (I've seen both). But `sed -i.bak 's/\000//g filename` would remove every occurrence of the null in the file (in place) while saving a backup of the original in filename.bak. Check on the proper null designation. – David C. Rankin Apr 21 '15 at 09:15

2 Answers2

2

This can be done using Perl:

perl -pe 's/\000/ /g' input.txt > output.txt

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
  • 1
    asked and answered yourself within a second ? what are you doing in SO ? – deimus Apr 21 '15 at 09:05
  • 2
    @deimus It's perfectly legitimate to ask and answer yourself at the same time - it's an option provided when writing a question. – Tom Fenech Apr 21 '15 at 09:08
  • 1
    Waiting for upvotes said the spider to the fly... Gotta love the creativity, albeit misguided. – David C. Rankin Apr 21 '15 at 09:09
  • 1
    There is an option "Answer your own question" - check it out. I'm just "documenting" the answer I need, because i don't want to look for it again after a week. Also someone might give me a better/faster solution. – Svetlin Zarev Apr 21 '15 at 09:10
  • @TomFenech sure, it is, But OP didn't really need the community help here at SO at all, as he did it right within a second :) – deimus Apr 21 '15 at 09:10
  • 1
    Actually someone might provide me with a better answer, as TomFrench just did :) I don't know if his approach is faster, but at least it seems much more easier to understand and remember :) – Svetlin Zarev Apr 21 '15 at 09:14
  • @SvetlinZarev Please get me correctly I don't have anything against, but for "self-documentation" purpose probably blogging is more appropriate. Because SO is not intended for how-to-do-instructions – deimus Apr 21 '15 at 09:16
  • 1
    I see your point, but this might help someone else too. Also it does not violate the SO rules, else it would not be possible to answer *exactly at the same time* as you've posted the question – Svetlin Zarev Apr 21 '15 at 09:19
  • @SvetlinZarev sure it is not possible. Would be more "nice" to place the answer you know into the question, and wait for a better solution. Otherwise its smelling with up-vote hunting. Anyway I don't want to stuck with this off-topic here. Everybody has his own approach to the things :) – deimus Apr 21 '15 at 09:23
  • 2
    @deimus there is literally an option to do this... Why on earth would you put an answer inside the question when this section is for answers. –  Apr 21 '15 at 09:26
  • You can join the discussion here: http://meta.stackoverflow.com/questions/290937/in-what-cases-its-acceptable-to-answer-your-own-question – Svetlin Zarev Apr 21 '15 at 09:30
  • @JID consider it like this : Hey guys I've following problem, I know following solution to it, but probably somebody can suggest smth better ? – deimus Apr 21 '15 at 09:31
  • @deimus Consider it like this : Hey guys i've had a problem i've been struggling with, i have now solved it and want to share my knowledge. Someone may have a better answer though so i'll post the question and my solution and see what others come up with. Although to be fair this is a pretty basic question and may be a repost/rehash of a similar one. –  Apr 21 '15 at 09:34
  • 1
    @JID also a good way to go with – deimus Apr 21 '15 at 10:43
1

You can use tr to do this:

tr '\000' ' ' file
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141