grep奋斗 - 只有整个路径匹配

时间:2018-03-24 23:30:01

标签: bash path grep

我已经从文件名中分隔(通过":"符号)路径,以便更容易地进行grep。我已经阅读了很多问题,但在这种情况下没有提出任何建议的解决方案。

我的清单:

  1 /c/Users/Michael : tutorial_2.sh 
  2 /c/Users/Michael : tutorial_1.sh 
  3 /c/Users/Michael : tutorial_3.sh 
  4 /c/Users/Michael : tutorial_7.sh 
  5 /c/Users/Michael/desktop/here : myfile.sh 
  6 /c/Users/Michael : tutorial_7.sh 
  7 /c/Users/Michael/desktop/here : myfile.sh 
  8 /c/Users/Michael : tutorial_7.sh 
  9 /c/Users/Michael/desktop/here : myfile.sh 
 10 /c/Users/Michael : tutorial_7.sh 
 11 /c/Users/Michael/desktop/here @ myfile.sh

我需要的输出:

  /c/Users/Michael : tutorial_2.sh 
  /c/Users/Michael : tutorial_1.sh 
  /c/Users/Michael : tutorial_3.sh 
  /c/Users/Michael : tutorial_7.sh
  /c/Users/Michael : tutorial_7.sh 
  /c/Users/Michael : tutorial_7.sh 
  /c/Users/Michael : tutorial_7.sh

当我这样的时候:

$ grep /c/Users/Michael ~/desktop/list

输出为:

The whole list (prints all records)

也尝试过:

grep -E '(^|\s)/c/Users/Michael($|/s)' ~/desktop/list

输出为:

nothing

也尝试过:

grep "\<"/c/Users/Michael"\>" ~/desktop/list

输出为:

nothing

也尝试过:

$ grep -w /c/Users/Michael ~/desktop/list

输出为:

The whole list (prints all records)

3 个答案:

答案 0 :(得分:1)

只需将空格添加到您正在寻找的模式中:

grep "/c/Users/Michael " ~/desktop/list

答案 1 :(得分:0)

\之后添加空格(Michael)并使用-o

$ grep -o /c/Users/Michael\ .* file
/c/Users/Michael : tutorial_2.sh 
/c/Users/Michael : tutorial_1.sh 
/c/Users/Michael : tutorial_3.sh 
/c/Users/Michael : tutorial_7.sh 
/c/Users/Michael : tutorial_7.sh 
/c/Users/Michael : tutorial_7.sh 
/c/Users/Michael : tutorial_7.sh 

答案 2 :(得分:0)

使用awk

awk '$2 == "/c/Users/Michael" { print $2" "$3" " $4 }' file