2

This question already has an answer here:

I have a text file contain numbers as follows:

34 77 1716 150?.2 67.5892 11.9691 23 1?6 83
35 78 0 0 0 0 0 0 0
36 79 0 0 0 0 0 0 0
37 80 0 0 ? ? 0 0 0
38 81 0 0 0 ? 0 0 0
39 82 0 0 0 0 ? 0 ?
40 85 169 152.8 81.5917 22.3759 18 118 100
41 251 1412 131? 97.7358 16.6563 37 126 89
42 252 578 488.5 88.?502 23.9728 29 124 95
43 253 585 518.6 95.4444 19.6661 19 119 100
44 254 576 533.2 96.4271 18.5693 13 119 106
45 255 1424 1313.3 94.7584 21.7414 14 146 132

I would like to replace every ? with 0 and every 0 with ? at the same time, so the table above look like this:

34 77 1716 15?0.2 67.5892 11.9691 23 106 83
35 78 ? ? ? ? ? ? ?
36 79 ? ? ? ? ? ? ?
37 8? ? ? 0 0 ? ? ?
38 81 ? ? ? 0 ? ? ?
39 82 ? ? ? ? 0 ? 0
4? 85 169 152.8 81.5917 22.3759 18 118 1??
41 251 1412 1310 97.7358 16.6563 37 126 89
42 252 578 488.5 88.05?2 23.9728 29 124 95
43 253 585 518.6 95.4444 19.6661 19 119 1??
44 254 576 533.2 96.4271 18.5693 13 119 1?6
45 255 1424 1313.3 94.7584 21.7414 14 146 132

How can I do it?

marked as duplicate by don_crissti, msp9011, filbranden, countermode, RalfFriedl Sep 6 '18 at 5:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    @Kasper can you share the expected result as well.. – msp9011 Sep 4 '18 at 19:02
7

I think, since you are only swapping singular characters, tr may be a good tool for the job.

Try something like this:

tr '0?' '?0' < log.txt
6

Since you're looking to swap single characters, you can use the y command in sed to transliterate:

sed 'y/?0/0?/' infile
0

Let's say that this text is in a file called log.txt, simply run this command:

sed "s/?/tmp/g;s/0/?/g;s/tmp/0/g" log.txt
  • 4
    wouldn't this have the problem where after converting all ? to 0, you now convert all 0 to ? including the ones that had just been converted from ?? – Christian Gibbons Sep 4 '18 at 17:23
  • Ahh !! The OP changed the table, I will update the command!! – user88036 Sep 4 '18 at 17:27