如何使用grep查找给定的字符串

时间:2022-09-11 19:17:51

I read some examples from * about this problem but none of them helped me.

我从*中读到了一些关于这个问题的例子,但没有一个帮助过我。

I have a folder where are some sql files. What I want to do is to go through all the sql files and searching for phrase and check if it exist or not. And the main problem is to write a grep expression of this phrase.

我有一个文件夹,其中有一些sql文件。我想要做的是浏览所有sql文件并搜索短语并检查它是否存在。主要问题是写一个这个短语的grep表达式。

The phrase have a pattern:

这句话有一个模式:

&uniq...

(for example: &uniq.hgk56kh/gfgtk or &uniqjfru5u5gggggt and so on)

(例如:&uniq.hgk56kh / gfgtk或&uniqjfru5u5gggggt等)

After &uniq could be only 21 signs, so the whole expression could have maximum 26 signs.

在&uniq之后只有21个符号,所以整个表达式最多可以有26个符号。

Do You have any idea how would I go about doing this?

你有什么想法我会这样做吗?

My examples which did't work:

我的例子不起作用:

for f in `ls $WORKDIR/*.sql`
do
if [ $(grep '[&uniq.*]\{1,26}' $f) == 0 ]; then echo
if [ $(grep -w '[&uniq.*{1,26}]' $f) == 0 ]; then echo
if [ $(grep -E '[&uniq.[[:alnum:]]{1,26}}' $f) == 0 ]; then echo
if [ $(grep -E '&uniq.[[:alnum:]]{1,26}' $f) == 0 ]; then echo
if [ $(grep -E '&uniq.[A-Za-z0-9]{1,26}' $f) == 0 ]; then echo
if [ $(grep -E '[&uniq.[A-Za-z0-9]{1,26}]' $f) == 0 ]; then echo
if [ $(grep -w '[&uniq.[A-Za-z0-9]\{1,26\}]' $f) == 0 ]; then echo
if [ $(grep -w '[&uniq.*\{1,26\}]' $f) == 0 ]; then echo
if [ $(grep -w '[&uniq.*}\{1,10\}' $f) == 0 ]; then echo
if [ $(grep -E '[&uniq.[A-Za-z0-9]{1,26}' $f) == 0 ]; then echo
if [ $(grep -E '[&uniq.*{1,26}' $f) == 0 ]; then echo
if [ $(grep -E '&uniq.*'{1,26} $f) == 0 ]; then echo
if [ $(grep -E '&uniq.*.\{1,26\}' $f) == 0 ]; then echo
if [ $(grep -E '&uniq.[A-Za-z0-9]{1,26}' ~/$f) == 0 ]; then echo
if [ $(grep -e '[&uniq.*]\{1,26}' $f) == 0 ]; then echo
done

Some examples of input: Correct: &uniq._short_examples &uniq._ifs21t &uniq._FM_Z2IO_RHHU &uniq._kontr_save Incorrect: &uniq.another_example_of_way_to_long_string &uniq.fhtygjygjyj767kjuhkhk87k8ukgfh56yhgyj76jujhyjk

输入的一些示例:正确:&uniq._short_examples&uniq._ifs21t&uniq._FM_Z2IO_RHHU&uniq._kontr_save错误:&uniq.another_example_of_way_to_long_string&uniq.fhtygjygjyj767kjuhkhk87k8ukgfh56yhgyj76jujhyjk

Between this examples are some typical sql files lines like this:

这个例子之间是一些典型的sql文件行,如下所示:

SELECT  a.YY,
        a.RRX_ID,
        a.MSRG_TYPE,
        a.TGHSEB,
        a.PROVIDERHJJ,
        a.SALE_PROVIDERYTR,
        a.REQUEST_PPL,
        a.CONTENT_NAME, -- Name of content
        a.CONTENT_TYPE,  -- Name of content2
        b.NET_AMOUNT,
        a.SALE_MODEL_FROM_PAST,   
        a.SERVICE_MODE_ON,  
        a.RESPONSE_STATUS_CODE,
        a.ERROR_DESC_FILE
FROM    BBV.RAQ45_TJKIO a,
        (SELECT * from &uniq.FGGT8IO) b

1 个解决方案

#1


I would suggest using the following:

我建议使用以下内容:

for f in /path/to/sql/files/*.sql; do
    if grep -q '&uniq.\{1,21\}' "$f"; then
        echo "match in file $f"
    fi
done

This matches "&uniq", followed by between 1 and 21 characters in any of the files in the directory $f. The -q switch to grep means that there is no output and the return code of grep indicates whether there was a match or not.

这匹配“&uniq”,后跟目录$ f中的任何文件中的1到21个字符。 -q切换到grep意味着没有输出,grep的返回代码表示是否匹配。

Presumably you also want to add some boundaries to either side of the pattern, so that you don't match things like abc&uniqabc123abc123abc123 123456789. The best way to do this depends on your input and the version of grep that you are using.

大概你也想在模式的任何一边添加一些边界,这样你就不会匹配像abc和uniqabc123abc123abc123 123456789这样的东西。最好的方法取决于你的输入和你正在使用的grep版本。

Judging by your example input, you could use the pattern ' &uniq.\{1,21\})' to match the pattern with a space before and a closing ) afterward.

根据您的示例输入判断,您可以使用模式'&uniq。\ {1,21 \})'将模式与之前的空格和结束后的空格匹配。

Based on the extra info in the comments, it looks like you should change the pattern to something like '&uniq\.[[:alnum:]]\{1,21\}[^[:alnum:]]', in order to match a literal dot after &uniq, between 1 and 21 alphanumeric characters, then any non-alphanumeric character.

根据评论中的额外信息,您应该将模式更改为'&uniq \。[[:alnum:]] \ {1,21 \} [^ [:alnum:]]',按顺序匹配&uniq之后的文字点,1到21个字母数字字符,然后是任何非字母数字字符。

#1


I would suggest using the following:

我建议使用以下内容:

for f in /path/to/sql/files/*.sql; do
    if grep -q '&uniq.\{1,21\}' "$f"; then
        echo "match in file $f"
    fi
done

This matches "&uniq", followed by between 1 and 21 characters in any of the files in the directory $f. The -q switch to grep means that there is no output and the return code of grep indicates whether there was a match or not.

这匹配“&uniq”,后跟目录$ f中的任何文件中的1到21个字符。 -q切换到grep意味着没有输出,grep的返回代码表示是否匹配。

Presumably you also want to add some boundaries to either side of the pattern, so that you don't match things like abc&uniqabc123abc123abc123 123456789. The best way to do this depends on your input and the version of grep that you are using.

大概你也想在模式的任何一边添加一些边界,这样你就不会匹配像abc和uniqabc123abc123abc123 123456789这样的东西。最好的方法取决于你的输入和你正在使用的grep版本。

Judging by your example input, you could use the pattern ' &uniq.\{1,21\})' to match the pattern with a space before and a closing ) afterward.

根据您的示例输入判断,您可以使用模式'&uniq。\ {1,21 \})'将模式与之前的空格和结束后的空格匹配。

Based on the extra info in the comments, it looks like you should change the pattern to something like '&uniq\.[[:alnum:]]\{1,21\}[^[:alnum:]]', in order to match a literal dot after &uniq, between 1 and 21 alphanumeric characters, then any non-alphanumeric character.

根据评论中的额外信息,您应该将模式更改为'&uniq \。[[:alnum:]] \ {1,21 \} [^ [:alnum:]]',按顺序匹配&uniq之后的文字点,1到21个字母数字字符,然后是任何非字母数字字符。