Replace spaces with hyphens in a text file [duplicate]












0
















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









share|improve this 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 of n for newlines replace spaces with - (hyphen/minus character).

    – karel
    Jan 21 at 8:53


















0
















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









share|improve this 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 of n for newlines replace spaces with - (hyphen/minus character).

    – karel
    Jan 21 at 8:53
















0












0








0









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









share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 of n for newlines replace spaces with - (hyphen/minus character).

    – karel
    Jan 21 at 8:53
















  • 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










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












1 Answer
1






active

oldest

votes


















3
















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





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3
















    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





    share|improve this answer






























      3
















      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





      share|improve this answer




























        3












        3








        3









        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





        share|improve this answer

















        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 21 at 9:48









        dessert

        22.8k56398




        22.8k56398










        answered Jan 21 at 8:58









        Sergiy KolodyazhnyySergiy Kolodyazhnyy

        71.8k9148314




        71.8k9148314















            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?