grep确切词匹配(

编程入门 行业动态 更新时间:2024-10-20 06:21:56
grep确切词匹配(-w)不适用于文本文件中的文件路径(grep exact word match (-w) does not work with file paths in a text file)

我试图在包含类似'ls -l'输出的文件中使用fullpath grep找到文件名,但它无法正确匹配它。

在执行字符串搜索的shell脚本中行

pcline=`grep -w "$file1" $file2` # grep for file1 in file2 contents

如果我回应命令,命令的输出如下所示

grep -w run /home/rajesh/rootfs.layout Expected lrwxrwxrwx 1 root root 3 Aug 28 run Actual lrwxrwxrwx 1 root root 7 Aug 28 bin/run-parts lrwxrwxrwx 1 root root 3 Aug 28 run -rwxr-xr-x 1 root root 303 Aug 28 tests/aes/run.sh -rwxr-xr-x 1 root root 445 Aug 28 tests/auto_ui/run.sh -rwxr-xr-x 1 root root 320 Aug 28 tests/available_memory/run.sh -rwxr-xr-x 1 root root 308 Aug 28 tests/fonts/run.sh -rwxr-xr-x 1 root root 309 Aug 28 tests/html_config_page/run.sh -rwxr-xr-x 1 root root 361 Aug 28 tests/ipc/run.sh -rwxr-xr-x 1 root root 304 Aug 28 tests/JSON/run.sh -rwxr-xr-x 1 root root 303 Aug 28 tests/log4cplus_cpp/run.sh -rwxr-xr-x 1 root root 301 Aug 28 tests/log4cplus_c/run.sh -rwxr-xr-x 1 root root 751 Aug 28 tests/msm_basic/run.sh -rwxr-xr-x 1 root root 472 Aug 28 tests/res_man_dependency/run.sh -rwxr-xr-x 1 root root 465 Aug 28 tests/res_man_ipc/run.sh -rwxr-xr-x 1 root root 789 Aug 28 tests/res_man_multi_process/run.sh -rwxr-xr-x 1 root root 469 Aug 28 tests/res_man_private_client/run.sh -rwxr-xr-x 1 root root 492 Aug 28 tests/res_man_public_client/run.sh -rwxr-xr-x 1 root root 311 Aug 28 tests/virt_mem_config/run.sh lrwxrwxrwx 1 root root 6 Aug 28 var/run]

我试过的诀窍是添加一个空白区域,这在输入文件中是有保证的,这在控制台中工作,但不是在将它分配给变量时。

grep " tests/aes/run.sh" /home/rajesh/rootfs.layout

在脚本中行

pcline=`grep \"" $file1"\" $file2` # grep for file1 in file2 contents

请让我知道,如果我在这个脚本中犯了任何错误。

Im trying to grep for a filename with fullpath in a file which contains something like 'ls -l' output, but it fails to match it correctly.

Line in the shell script which does the string search

pcline=`grep -w "$file1" $file2` # grep for file1 in file2 contents

if i echo the command, the output of command looks like below

grep -w run /home/rajesh/rootfs.layout Expected lrwxrwxrwx 1 root root 3 Aug 28 run Actual lrwxrwxrwx 1 root root 7 Aug 28 bin/run-parts lrwxrwxrwx 1 root root 3 Aug 28 run -rwxr-xr-x 1 root root 303 Aug 28 tests/aes/run.sh -rwxr-xr-x 1 root root 445 Aug 28 tests/auto_ui/run.sh -rwxr-xr-x 1 root root 320 Aug 28 tests/available_memory/run.sh -rwxr-xr-x 1 root root 308 Aug 28 tests/fonts/run.sh -rwxr-xr-x 1 root root 309 Aug 28 tests/html_config_page/run.sh -rwxr-xr-x 1 root root 361 Aug 28 tests/ipc/run.sh -rwxr-xr-x 1 root root 304 Aug 28 tests/JSON/run.sh -rwxr-xr-x 1 root root 303 Aug 28 tests/log4cplus_cpp/run.sh -rwxr-xr-x 1 root root 301 Aug 28 tests/log4cplus_c/run.sh -rwxr-xr-x 1 root root 751 Aug 28 tests/msm_basic/run.sh -rwxr-xr-x 1 root root 472 Aug 28 tests/res_man_dependency/run.sh -rwxr-xr-x 1 root root 465 Aug 28 tests/res_man_ipc/run.sh -rwxr-xr-x 1 root root 789 Aug 28 tests/res_man_multi_process/run.sh -rwxr-xr-x 1 root root 469 Aug 28 tests/res_man_private_client/run.sh -rwxr-xr-x 1 root root 492 Aug 28 tests/res_man_public_client/run.sh -rwxr-xr-x 1 root root 311 Aug 28 tests/virt_mem_config/run.sh lrwxrwxrwx 1 root root 6 Aug 28 var/run]

The trick i tried is to add a white space, which is guaranteed in my input file, this works in console, but not when it is assigned to a variable.

grep " tests/aes/run.sh" /home/rajesh/rootfs.layout

Line in the script

pcline=`grep \"" $file1"\" $file2` # grep for file1 in file2 contents

Please let me know if i have committed any errors in this script.

最满意答案

你可以像这样使用egrep :

egrep "(^| )$file1( |$)" "$file2"

如果file1="run"那么上面的命令将匹配以行开头或空格开头的字符串,然后是空格或行结束。

You can use egrep like this:

egrep "(^| )$file1( |$)" "$file2"

If file1="run" then above command will match string run preceded by line start or space and followed by space or line end.

更多推荐

本文发布于:2023-04-28 03:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:确切   grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!