Replacing first occurence in every line

前端 未结 6 1004
囚心锁ツ
囚心锁ツ 2021-01-27 01:11

Supposing I have a document formatted like this:

word1 word2 word3
word4 word5 word6
word7 word8

How do I use sed to replace the f

相关标签:
6条回答
  • 2021-01-27 01:30

    Using awk

    awk '{sub(/ /,", ")}1' file
    word1, word2 word3
    word4, word5 word6
    word7, word8
    
    0 讨论(0)
  • 2021-01-27 01:32
    sed 's/\([^[:blank:]]\{1,\}\)/\1,/' YourFile
    

    Add a , after first block of "non space", so start of line could be starting with space, it still work and also if line is without work but only with space (no , in this case)

    0 讨论(0)
  • 2021-01-27 01:43

    Simple:

    sed -i 's/ /, /' your_file
    

    This looks for the first occurrence of a space, replaces it with a comma and space, then moves to the next line.

    0 讨论(0)
  • 2021-01-27 01:43

    how about:

    sed -e 's/\s\+/, /'
    

    output:

    word1, word2 word3
    word4, word5 word6
    word7, word8
    
    0 讨论(0)
  • 2021-01-27 01:49

    echo FRIEND TOO MUCH |sed 's/ /, /'

    FRIEND, TOO MUCH

    0 讨论(0)
  • 2021-01-27 01:50
    echo word1 word2 word3|sed -r 's/(\s)/,\1/'
    

    output:

    word1, word2 word3
    
    0 讨论(0)
提交回复
热议问题