gpt4 book ai didi

awk - grep 注释文件中的单词

转载 作者:行者123 更新时间:2023-12-03 04:21:39 26 4
gpt4 key购买 nike

我想从文件中 grep 一个特定的命令/单词,例如 int
但我想消除所有注释的行。
(我想忽略 int 是否在 # 之后)。

我的文件内容:

int a
def
abc int
adbc asdfj #int
abc # int
# int abc
abc int #
int # abc

我想要输出为:

int a
abc int
abc int #
int # abc

我尝试使用grep -e "int"| grep -v -e "#" 。但问题是 int # abc 也被消除了。

最佳答案

我看到一些已删除的答案以及以下有效的单正则表达式答案:grep '^[^#]*\<int\>'

grep '^[^#]*\<int\>' <<END
int a
def
abc int
adbc asdfj #int
abc # int
abc int #
int # abc
print abc # int -- should not see this line
END
int a
abc int
abc int #
int # abc

你有int吗? #两侧?这种情况下你该怎么办?

$ echo "int foo # int bar" | grep '^[^#]*\<int\>'
int foo # int bar
<小时/>

要查看文件中是否使用了“int”,请使用 grep 的 -q选项:

if grep -q '^[^#]*\<int\>' file; then 
echo "I have an 'int'"
else
echo "No int here"
fi

要将单词作为参数传递,您需要双引号,并转义反斜杠:

type="int"
if grep -q "^[^#]*\\<$type\\>"; then ...

关于awk - grep 注释文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485131/

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