开发者

Stripping lines which don't include a text phrase

Starting with a .txt file with lines li开发者_开发百科ke

adfgnfghqueendsfgdfg
dfgdfgdfg
gdfgdfgfhsfqueenjkhkjhkjg
hksad,jfhgkfdg

How would I strip the lines which don't have 'queen' in them? (preferably using an OS X Terminal command)


You can use sed as:

  1. Print only lines that have queen in them and make changes inline.

    sed  -i -n '/queen/p' file
    
  2. Delete lines that do not have queen in them and make changes inline.

    sed  -i '/queen/!d' file
    

The above command delete if they find the word queen anywhere in the line, even as a part of another word. If this is not what you want and you want to delete only those lines where queen appears a separate word and not as part of another word you can make use of word boundaries \bas:

sed  -i -n '/\bqueen\b/p' file
sed  -i '/\bqueen\b/!d' file


You can use grep. For example:

grep "queen" file.txt > newfile.txt


sed '/queen/ d' infile.txt > outfile.txt
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜