String sed/awk-从文件中删除除特定匹配模式以外的所有模式

String sed/awk-从文件中删除除特定匹配模式以外的所有模式,string,makefile,sed,awk,String,Makefile,Sed,Awk,我想转换如下生成文件: test.o: var_one.h var_two.h test2.o: another_header.h var_three.h archive/something_random.h 为此: test.o: var_one.h var_two.h test2.o: var_three.h 也就是说,我想删除任何不以搜索字符串“var\ux”开头的文件名 当我不需要搜索字符串时,我似乎找不到很多关于模式匹配的sed信息?你真的需要awk来完成这类工作。使用sed,

我想转换如下生成文件:

test.o: var_one.h var_two.h

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

test.o: var_one.h var_two.h

test2.o: var_three.h
也就是说,我想删除任何不以搜索字符串“var\ux”开头的文件名


当我不需要搜索字符串时,我似乎找不到很多关于模式匹配的sed信息?

你真的需要awk来完成这类工作。使用sed,试图删除与字符串不匹配的单词将是一个痛苦的(可能是丑陋的)脚本

awk '{for(i=2;i<=NF;i++)$i !~ /var_.*\.h/ && $i=""}1' Makefile
输出
$awk'{for(i=2;除sed或awk之外的其他脚本语言可能是什么?
$ cat Makefile
test.o: var_one.h var_two.h

test2.o: another_header.h var_three.h archive/something_random.h
$ 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