Wednesday, May 11, 2011

vi or sed - Delete till end of line from pattern

Here's the regex that will match everything from the pattern until a newline is found.

Say you want to remove everything after "here" from this sample file.

code here    where?
code here   right here!
code here         look every where.
Used regex
s/here.*//
Explanation:
.*  
dot matches any character including spaces, numbers, alphabets but not new line.
asterisk matches any number of occurrences of a character

s/here.*/meow/ will result in

code meow
code meow
code meow

1 comment:

  1. Thanks a lot! I was looking for something like that in 0'Reilly book about sed, that famous one, but could not find what I really needed. A search in the internet got to this blog, that I will follow from now one.

    ReplyDelete