【问题标题】:How to do grep of string (which contains file directory path as value ) in a input file如何在输入文件中对字符串(包含文件目录路径作为值)进行 grep
【发布时间】:2021-02-10 13:13:45
【问题描述】:

我的字符串值为/tmp/Sne/test1.list,我有一个名为all_tests.txt的文件

此 all_tests.txt 包含以下行:

/tmp/Sne/test1.list
/tmp/Sne/test2.list
/tmp/Sne/test3.list
/tmp/Sne/test4.list

如何使用grep 来搜索字符串值/tmp/Sne/test1.list 是否存在于all_tests.txt 中。 如果存在,那么我需要echo 一个文件存在的消息。

我尝试使用:

if [ grep -Fxq "$p" $Input_file ]; then echo "This file list is already checked"

其中$p/tmp/Sne/test1.list$Input_fileall_tests.txt

这不起作用。

请寻求帮助。

【问题讨论】:

  • 括号没有意义。 if 执行 then 部分,如果调用的程序返回退出代码 0。
  • This is not working 是对问题的最糟糕的描述,在这里没有什么比你把车送到修理工那里修理而你告诉他们的只是This is not working 更有用。告诉我们它在什么方面不工作(无输出、错误输出、错误消息等),以便我们帮助您修复它。

标签: linux shell grep


【解决方案1】:

你快到了:

if grep -Fxq "$p" "$Input_file"; then echo "This file list is already checked"; fi

您正在混合使用两个命令:[ (man test) 和 grep

PS:用双引号将变量括起来 ($Input_file => "$Input_file"),除非您确定不能这样做。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2013-07-06
    • 1970-01-01
    • 2020-04-14
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    相关资源
    最近更新 更多