Sed to remove everything after “.” in file using * command?

前端 未结 3 378
我寻月下人不归
我寻月下人不归 2021-02-05 15:10

I have the following data.txt:

 95 flour.
 47 water.s
 etc..

I need to remove everything after the period (.) in the file to yield s

相关标签:
3条回答
  • 2021-02-05 15:49

    This is the simplest:

    sed "s/\..*//"
    

    And this is, I think, one of the best ways of doing it (better than pure bash or Python).

    0 讨论(0)
  • 2021-02-05 16:11

    Either escape the . with a backslash to get a literal ., or use brackets to define a character class:

    sed 's/\..*$//' data.txt > cleaned.txt
    sed 's/[.].*$//' data.txt > cleaned.txt
    

    You tried 's/\.*//', which is "zero or more literal dots", which is different from "literal dot followed by zero or more of anything", i.e. 's/\..*//'. I also added a $ for good measure.

    0 讨论(0)
  • 2021-02-05 16:12

    This seemed to work:

    sed "s/[.].*//" data.txt > cleaned.txt
    

    I would be interested to know how this can be done alternatively in bash and python, if anyone wouldn't mind sharing?

    Thanks!

    0 讨论(0)
提交回复
热议问题