[英]What is the difference between “$line” and “^$line”
There is a while read loop: 有一段时间的读取循环:
while read line; do
grep "^$line" file1
done < target
I should use "^$line"
to get the right answer. 我应该使用
"^$line"
来获得正确的答案。 I want to know what is the difference between "$line"
and "^$line"
? 我想知道
"$line"
和"^$line"
什么区别?
The while
loop is not relevant here, it's the grep
command that make the ^
significant. while
循环在这里grep
,是grep
命令使^
有意义。 The ^
character is a identifier in regular expressions. ^
字符是正则表达式中的标识符。 It matches the start of the line, Therefore grep "$line" file
matches any lines in the file where $line
is substring but grep "^$line"
only matches those that start with $line
. 它与行的开头匹配,因此
grep "$line" file
与$line
是子字符串的grep "$line" file
中的任何行匹配,而grep "^$line"
仅与以$line
开头的$line
匹配。
@sudo_O has the right answer for your question. @sudo_O为您的问题提供了正确的答案。 However, you can omit the loop altogether:
但是,您可以完全省略循环:
grep -f target file1
It's way more efficient with just a single call to grep. 只需调用grep即可更高效。 If you need the line anchor:
如果您需要线锚:
grep -f <(sed 's/^/^/' target) file1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.