开发者

sed / awk - remove all but a specific matching pattern from a file

I want to convert a makefile like this:

test.o: var_one.h var_two.h

test2.o: another_header.h var_three.h archive/something_random.h

to this:

test.o: var_one.h var_two.h

test2.o: var_three.h

ie, I want to remove any filename that doesn't 开发者_如何学Pythonstart with the search string "var_"

I can't seem to find a lot of information for sed about pattern matching when not wanting the search string?


You really need awk for this type of job. With sed, attempting to remove words that don't match a string would be a painful (and probably ugly) script to write.

awk '{for(i=2;i<=NF;i++)$i !~ /var_.*\.h/ && $i=""}1' Makefile

Input

$ cat Makefile
test.o: var_one.h var_two.h

test2.o: another_header.h var_three.h archive/something_random.h

Output

$ awk '{for(i=2;i<=NF;i++)$i !~ /var_.*\.h/ && $i=""}1' Makefile
test.o: var_one.h var_two.h

test2.o:  var_three.h
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜