简体   繁体   English

如何在grep中转义管道字符?

[英]how to escape the pipe character in grep?

I have a file in this format: 我有这种格式的文件:

coupait ||| eastern ||| 0.045454545454545456 2.718 ||| 0-0 ||| 
instaurer ||| characteristic ||| 5.797101449275362E-4 2.718 ||| 0-0 |||
tiendrait ||| fails ||| 0.005 2.718 ||| 0-0 |||

and I would like to search for a pair of words that are saved in two variables and which are seperated by |||. 我想搜索一对保存在两个变量中并由|||分隔的单词。 I tried many alternatives in vain like : 我尝试了很多替代方案,如:

grep "$word1 ||| $word2 |||" $file

I tried escaping the pipe characters like this: 我尝试转义管道字符,如下所示:

grep '$word1 \|\|\| $word2 \|\|\|' $file

How can I match the pipe in the file? 如何匹配文件中的管道?

Use grep -F - the variables will be interpreted by the shell and replaced with their respective values, then the quoted string (including the pipes) will be interpreted as a fixed string. 使用grep -F - 变量将由shell解释并替换为它们各自的值,然后引用的字符串(包括管道)将被解释为固定字符串。 Example: 例:

$ cat file.txt
coupait ||| eastern ||| 0.045454545454545456 2.718 ||| 0-0 ||| 
instaurer ||| characteristic ||| 5.797101449275362E-4 2.718 ||| 0-0 |||
tiendrait ||| fails ||| 0.005 2.718 ||| 0-0 |||
$ word1=coupait
$ word2=eastern
$ grep -F "$word1 ||| $word2 |||" file.txt
coupait ||| eastern ||| 0.045454545454545456 2.718 ||| 0-0 ||| 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM