当单词包含破折号时,GREP的完整单词不起作用



我使用 -w标志的整个单词正在 grep,并且它无法正常工作。我认为这是因为源单词包含" dash"(" - "(字符:

echo "sdf-a" > /tmp/grep.txt
~/Developer : cat /tmp/grep.txt 
sdf-a

不应返回任何东西:

cat /tmp/grep.txt | grep -w a
sdf-a <<<<<<<<<<<<<<< BAD

我还尝试了" "选项:

cat /tmp/grep.txt | grep  "<a>"
sdf-a << Should not return it

当我从源词中删除仪表板时:

~/Developer/ : echo "sdfa" > /tmp/grep.txt
~/Developer/ : cat /tmp/grep.txt | grep  "<a>" // nothing returned as expected
~/Developer/ : cat /tmp/grep.txt | grep -w a // nothing returned as expected

诉诸于一个小的红宝石例程(出于我的目的(,它将与多个破折号匹配:

def grep(file,val)
    res = []
    text = File.open(file).read
    text.each_line { |l|
      if l.include?(val)
        res.push(l)
      end
    }
    if res.count == 0
      return nil
    end
    return res
  end

最新更新