How to replace space with comma using sed?

前端 未结 8 701
-上瘾入骨i
-上瘾入骨i 2020-12-30 22:52

I would like to replace the empty space between each and every field with comma delimiter.Could someone let me know how can I do this.I tried the below command but it doesn\

相关标签:
8条回答
  • 2020-12-30 22:56

    IF your data includes an arbitrary sequence of blank characters (tab, space), and you want to replace each sequence with one comma, use the following:

    sed 's/[\t ]+/,/g' input_file
    

    or

    sed -r 's/[[:blank:]]+/,/g' input_file
    

    If you want to replace sequence of space characters, which includes other characters such as carriage return and backspace, etc, then use the following:

    sed -r 's/[[:space:]]+/,/g' input_file
    
    0 讨论(0)
  • 2020-12-30 22:57

    Inside vim, you want to type when in normal (command) mode:

    :%s/ /,/g
    

    On the terminal prompt, you can use sed to perform this on a file:

    sed -i 's/\ /,/g' input_file
    

    Note: the -i option to sed means "in-place edit", as in that it will modify the input file.

    0 讨论(0)
  • 2020-12-30 23:05

    I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:

    tr , '\n' < file
    
    0 讨论(0)
  • 2020-12-30 23:05

    On Linux use below to test (it would replace the whitespaces with comma)

     sed 's/\s/,/g' /tmp/test.txt | head
    

    later you can take the output into the file using below command:

    sed 's/\s/,/g' /tmp/test.txt > /tmp/test_final.txt
    

    PS: test is the file which you want to use

    0 讨论(0)
  • 2020-12-30 23:12

    Try the following command and it should work out for you.

    sed "s/\s/,/g" orignalFive.csv > editedFinal.csv
    
    0 讨论(0)
  • 2020-12-30 23:14

    If you are talking about sed, this works:

    sed -e "s/ /,/g" < a.txt
    

    In vim, use same regex to replace:

    s/ /,/g
    
    0 讨论(0)
提交回复
热议问题