【问题标题】:sed / awk - remove all but a specific matching pattern from a filesed / awk - 从文件中删除除特定匹配模式之外的所有模式
【发布时间】:2011-05-25 07:40:44
【问题描述】:

我想像这样转换一个makefile:

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_”开头的文件名

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

【问题讨论】:

  • 除了 sed 或 awk 什么都没有?可能是一种脚本语言?

标签: string makefile sed awk


【解决方案1】:

这种类型的工作确实需要 awk。使用 sed,尝试删除与字符串不匹配的单词将是一个痛苦的(并且可能是丑陋的)脚本。

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

输入

$ 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

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多