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:
Print only lines that have
queen
in them and make changes inline.sed -i -n '/queen/p' file
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 \b
as:
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
精彩评论