gpt4 book ai didi

string - sed/awk - 从文件中删除除特定匹配模式之外的所有内容

转载 作者:行者123 更新时间:2023-12-02 06:24:29 25 4
gpt4 key购买 nike

我想转换这样的 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 有关模式匹配的大量信息?

最佳答案

对于这种类型的工作,你确实需要 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

关于string - sed/awk - 从文件中删除除特定匹配模式之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4440164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com