Replace spaces with hyphens in a text file [duplicate]
This question already has an answer here:
How to replace spaces with newlines/enter in a text-file?
6 answers
I have a text file as follows:
2018 11 20
2018 11 21
2018 11 22
2018 11 23
2018 11 24
2018 11 25
2018 11 26
2018 11 27
This file consists of year,month and day with three columns. I need to add hyphen -
between the year, month and day as follows:
2018-11-20
2018-11-21
2018-11-22
2018-11-23
2018-11-24
2018-11-25
2018-11-26
2018-11-27
text-processing
marked as duplicate by karel, Eric Carvalho, Charles Green, Anwar, Pilot6 Jan 30 at 18:49
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 |
This question already has an answer here:
How to replace spaces with newlines/enter in a text-file?
6 answers
I have a text file as follows:
2018 11 20
2018 11 21
2018 11 22
2018 11 23
2018 11 24
2018 11 25
2018 11 26
2018 11 27
This file consists of year,month and day with three columns. I need to add hyphen -
between the year, month and day as follows:
2018-11-20
2018-11-21
2018-11-22
2018-11-23
2018-11-24
2018-11-25
2018-11-26
2018-11-27
text-processing
marked as duplicate by karel, Eric Carvalho, Charles Green, Anwar, Pilot6 Jan 30 at 18:49
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.
5
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead ofn
for newlines replace spaces with-
(hyphen/minus character).
– karel
Jan 21 at 8:53
add a comment |
This question already has an answer here:
How to replace spaces with newlines/enter in a text-file?
6 answers
I have a text file as follows:
2018 11 20
2018 11 21
2018 11 22
2018 11 23
2018 11 24
2018 11 25
2018 11 26
2018 11 27
This file consists of year,month and day with three columns. I need to add hyphen -
between the year, month and day as follows:
2018-11-20
2018-11-21
2018-11-22
2018-11-23
2018-11-24
2018-11-25
2018-11-26
2018-11-27
text-processing
This question already has an answer here:
How to replace spaces with newlines/enter in a text-file?
6 answers
I have a text file as follows:
2018 11 20
2018 11 21
2018 11 22
2018 11 23
2018 11 24
2018 11 25
2018 11 26
2018 11 27
This file consists of year,month and day with three columns. I need to add hyphen -
between the year, month and day as follows:
2018-11-20
2018-11-21
2018-11-22
2018-11-23
2018-11-24
2018-11-25
2018-11-26
2018-11-27
This question already has an answer here:
How to replace spaces with newlines/enter in a text-file?
6 answers
text-processing
text-processing
edited Jan 22 at 20:49
Zanna
50.7k13135241
50.7k13135241
asked Jan 21 at 8:47
deepblue_86deepblue_86
5851023
5851023
marked as duplicate by karel, Eric Carvalho, Charles Green, Anwar, Pilot6 Jan 30 at 18:49
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 karel, Eric Carvalho, Charles Green, Anwar, Pilot6 Jan 30 at 18:49
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.
5
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead ofn
for newlines replace spaces with-
(hyphen/minus character).
– karel
Jan 21 at 8:53
add a comment |
5
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead ofn
for newlines replace spaces with-
(hyphen/minus character).
– karel
Jan 21 at 8:53
5
5
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead of
n
for newlines replace spaces with -
(hyphen/minus character).– karel
Jan 21 at 8:53
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead of
n
for newlines replace spaces with -
(hyphen/minus character).– karel
Jan 21 at 8:53
add a comment |
1 Answer
1
active
oldest
votes
sed
Assuming there's no spaces after the last column, we can do
sed 's/[[:blank:]]/-/g' input.txt
In case where there may be other characters and we specifically want to capture the pattern "sequence of digits, then sequence of spaces" we can also use group patterns in parenthesis and extended regex -r
:
sed -r 's/([[:digit:]]?+) ?+([[:digit:]]?+) ?+([[:digit:]]?+)/1-2-3/' input.txt
shell
In case we want shell-only solution:
while read -r year month day trash; do
printf "%s-%s-%sn" "$year" "$month" "$day"
done < input.txt
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed
Assuming there's no spaces after the last column, we can do
sed 's/[[:blank:]]/-/g' input.txt
In case where there may be other characters and we specifically want to capture the pattern "sequence of digits, then sequence of spaces" we can also use group patterns in parenthesis and extended regex -r
:
sed -r 's/([[:digit:]]?+) ?+([[:digit:]]?+) ?+([[:digit:]]?+)/1-2-3/' input.txt
shell
In case we want shell-only solution:
while read -r year month day trash; do
printf "%s-%s-%sn" "$year" "$month" "$day"
done < input.txt
add a comment |
sed
Assuming there's no spaces after the last column, we can do
sed 's/[[:blank:]]/-/g' input.txt
In case where there may be other characters and we specifically want to capture the pattern "sequence of digits, then sequence of spaces" we can also use group patterns in parenthesis and extended regex -r
:
sed -r 's/([[:digit:]]?+) ?+([[:digit:]]?+) ?+([[:digit:]]?+)/1-2-3/' input.txt
shell
In case we want shell-only solution:
while read -r year month day trash; do
printf "%s-%s-%sn" "$year" "$month" "$day"
done < input.txt
add a comment |
sed
Assuming there's no spaces after the last column, we can do
sed 's/[[:blank:]]/-/g' input.txt
In case where there may be other characters and we specifically want to capture the pattern "sequence of digits, then sequence of spaces" we can also use group patterns in parenthesis and extended regex -r
:
sed -r 's/([[:digit:]]?+) ?+([[:digit:]]?+) ?+([[:digit:]]?+)/1-2-3/' input.txt
shell
In case we want shell-only solution:
while read -r year month day trash; do
printf "%s-%s-%sn" "$year" "$month" "$day"
done < input.txt
sed
Assuming there's no spaces after the last column, we can do
sed 's/[[:blank:]]/-/g' input.txt
In case where there may be other characters and we specifically want to capture the pattern "sequence of digits, then sequence of spaces" we can also use group patterns in parenthesis and extended regex -r
:
sed -r 's/([[:digit:]]?+) ?+([[:digit:]]?+) ?+([[:digit:]]?+)/1-2-3/' input.txt
shell
In case we want shell-only solution:
while read -r year month day trash; do
printf "%s-%s-%sn" "$year" "$month" "$day"
done < input.txt
edited Jan 21 at 9:48
dessert
22.8k56398
22.8k56398
answered Jan 21 at 8:58
Sergiy KolodyazhnyySergiy Kolodyazhnyy
71.8k9148314
71.8k9148314
add a comment |
add a comment |
5
Possible duplicate of How to replace spaces with newlines/enter in a text-file? Instead of
n
for newlines replace spaces with-
(hyphen/minus character).– karel
Jan 21 at 8:53