Replacing two patterns in a text files at the same time [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
This question already has an answer here:
Cleanly swap all occurences of two strings using sed
3 answers
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?
replace text
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by don_crissti, SivaPrasath, Filipe Brandenburger, countermode, RalfFriedl Sep 6 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.
add a comment |Â
up vote
2
down vote
favorite
This question already has an answer here:
Cleanly swap all occurences of two strings using sed
3 answers
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?
replace text
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by don_crissti, SivaPrasath, Filipe Brandenburger, countermode, RalfFriedl Sep 6 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..
– SivaPrasath
Sep 4 at 19:02
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
Cleanly swap all occurences of two strings using sed
3 answers
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?
replace text
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This question already has an answer here:
Cleanly swap all occurences of two strings using sed
3 answers
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?
This question already has an answer here:
Cleanly swap all occurences of two strings using sed
3 answers
replace text
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Sep 5 at 5:01
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Sep 4 at 17:14
Kasper
12611
12611
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Kasper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by don_crissti, SivaPrasath, Filipe Brandenburger, countermode, RalfFriedl Sep 6 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.
marked as duplicate by don_crissti, SivaPrasath, Filipe Brandenburger, countermode, RalfFriedl Sep 6 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..
– SivaPrasath
Sep 4 at 19:02
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01
add a comment |Â
1
@Kasper can you share the expected result as well..
– SivaPrasath
Sep 4 at 19:02
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01
1
1
@Kasper can you share the expected result as well..
– SivaPrasath
Sep 4 at 19:02
@Kasper can you share the expected result as well..
– SivaPrasath
Sep 4 at 19:02
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
8
down vote
accepted
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
add a comment |Â
up vote
6
down vote
Since you're looking to swap single characters, you can use the y
command in sed to transliterate:
sed 'y/?0/0?/' infile
add a comment |Â
up vote
1
down vote
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?
to0
, you now convert all0
to?
including the ones that had just been converted from?
?
– Christian Gibbons
Sep 4 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
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
add a comment |Â
up vote
8
down vote
accepted
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
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
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
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
edited Sep 4 at 19:05
answered Sep 4 at 17:28
Christian Gibbons
23119
23119
add a comment |Â
add a comment |Â
up vote
6
down vote
Since you're looking to swap single characters, you can use the y
command in sed to transliterate:
sed 'y/?0/0?/' infile
add a comment |Â
up vote
6
down vote
Since you're looking to swap single characters, you can use the y
command in sed to transliterate:
sed 'y/?0/0?/' infile
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Since you're looking to swap single characters, you can use the y
command in sed to transliterate:
sed 'y/?0/0?/' infile
Since you're looking to swap single characters, you can use the y
command in sed to transliterate:
sed 'y/?0/0?/' infile
answered Sep 4 at 18:54


Benjamin W.
351110
351110
add a comment |Â
add a comment |Â
up vote
1
down vote
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?
to0
, you now convert all0
to?
including the ones that had just been converted from?
?
– Christian Gibbons
Sep 4 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
add a comment |Â
up vote
1
down vote
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?
to0
, you now convert all0
to?
including the ones that had just been converted from?
?
– Christian Gibbons
Sep 4 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
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
edited Sep 4 at 18:21
answered Sep 4 at 17:17
Goro
1,34641642
1,34641642
4
wouldn't this have the problem where after converting all?
to0
, you now convert all0
to?
including the ones that had just been converted from?
?
– Christian Gibbons
Sep 4 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
add a comment |Â
4
wouldn't this have the problem where after converting all?
to0
, you now convert all0
to?
including the ones that had just been converted from?
?
– Christian Gibbons
Sep 4 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
4
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 at 17:23
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 at 17:23
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
Ahh !! The OP changed the table, I will update the command!!
– Goro
Sep 4 at 17:27
add a comment |Â
1
@Kasper can you share the expected result as well..
– SivaPrasath
Sep 4 at 19:02
thank yo sir. I did update the question.
– Kasper
Sep 5 at 5:01