对包含带有制表符的字符串的变量进行grep [英] grep on a variable containing a string with a tab
问题描述
我有一个for循环,循环几行。每行都有两个字符串和一个分隔它们的标签,例如
I have a for loop that loops over a few lines. each line has two strings with a tab separating them e.g
chr11 105804693
当我将变量传递给grep并用文件搜索时,返回105804693:No such file or directory
。我的代码如下所示:
when I pass the variable to grep with a file to search in it returns "105804693: No such file or directory"
. my code looks like this:
换行$(cat list_of_lines);
做grep $ line file_to_search_in >> output_file;
完成
for line in $(cat list_of_lines); do grep $line file_to_search_in >> output_file; done
我尝试了 printf
变量并返回chr11
但是当我回应变量时,它返回chr11 105804693
I tried to printf
the variable and it returned "chr11"
but when I echoed the variable it returned "chr11 105804693"
如何在变量上调用grep并让它搜索整行?
How can I call grep on the variable and have it search for the full line?
推荐答案
You probably haven't added quotes around your variable having the search pattern,
search_pattern_variable="chr11 105804693"
grep "$search_pattern_variable" input-file
由于您的变量包含 chr11 105804693
,
shell
使用默认的 IFS
(字段分隔符)将单词拆分为多个单词已经使用 chr11
作为模式, 105804693
作为文件名,报告没有看到这样的文件。
Since your variable had the words chr11 105804693
, shell
split up the words into multiple words with the default IFS
(field-separator) and had used chr11
as the pattern and 105804693
as the file-name, which it is reporting not seeing such a file.
看看 Word-Splitting 表示 bash
。
这篇关于对包含带有制表符的字符串的变量进行grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!