grep: match all characters up to (not including) first blank space

前端 未结 4 785
南旧
南旧 2021-02-05 04:24

I have a text file that has the following format:

characters(that I want to keep) (space) characters(that I want to remove)

So for example:

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 04:54

    You are putting quantifier * at the wrong place.

    Try instead this: -

    grep '^[^\s]*' text1.txt > text2.txt
    

    or, even better: -

    grep '^\S*' text1.txt > text2.txt  
    

    \S means match non-whitespace character. And anchor ^ is used to match at the beginning of the line.

提交回复
热议问题