开发者

How to use a regular expression to remove lines without a word?

开发者 https://www.devze.com 2023-03-27 01:44 出处:网络
I am using textmate to edit a file. I would like to remove all the lines not containing a word. Here is an example.

I am using textmate to edit a file. I would like to remove all the lines not containing a word. Here is an example.

apple ipad
hp touch pad
samsung galaxy tab
motorola xoom

How can i remove all the line not containing the word "pad", and get this re开发者_运维百科sult, using Regular Expression??

apple ipad
hp touch pad

Thanks all.


Replace ^(?!.*pad.*).+$ with empty string


I'm not sure about doing this kind of thing using regular expressions but you could easily use grep to do this.

For example, if the file textfile contains this:

apple ipad
hp touch pad
samsung galaxy tab
motorola xoom

Open Terminal, and run this command:

grep pad textfile

It'll output this:

apple ipad
hp touch pad

If you want to save the output to a file you can do something like this:

grep pad textfile > filteredfile


This expression will select lines that contain the word pad: ^.*pad.*$

The ^ character indicates the start of a line, the $ character indicates the end, and .* allows for any number of characters surrounding the word.

This may be too wide-ranged for your purpose in its current state - more specific information is needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消