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

前端 未结 4 793
南旧
南旧 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 05:12

    I use egrep a lot to help "colorize" log lines, so I'm always looking for a new twist on regex. For me, the above works better by adding a \W like this:

    $ egrep --color '^\S*\W|bag' /tmp/barf -o
    foo
    bag
    hello
    bag
    keepthis
    (etc.)
    

    Problem is, my log files almost always are time-stamped, so I added a line to the example file:

    2013-06-11 date stamped line
    

    and then it doesn't work so well. So I reverted to my previous regex:

    egrep --color '^\w*\b|bag' /tmp/barf
    

    but the non-date-stamped lines revealed problems with that. It is hard to see this without colorization...

提交回复
热议问题