将grep输出保存到变量会更改值

huangapple go评论61阅读模式
英文:

Saving grep output to variable changes value

问题

I want to use grep to determine if a string contains a substring. (The plan is to use that result as the test command in a bash if statement.)

Here, I check the length of the output of grep . :

$ echo "abc" | grep "j" | wc -c
       0

Since wc -c shows zero, I know that grep returned an empty string.

But if I save output from grep to a variable before calling wc -c, I get a different value:

$ match=$(echo "abc" | grep "j")
$ echo "$match" | wc -c
       1

The output from grep now is a string with 1 character in it.

I suspect that it's a newline in there:

$ echo "$match"

$

Why is there now an extra character in $match, and how can I keep that from happening?

英文:

I want to use grep to determine if a string contains a substring. (The plan is to use that result as the test command in a bash if statement.)

Here, I check the length of the output of grep . :

$ echo "abc" | grep "j" | wc -c
       0

Since wc -c shows zero, I know that grep returned an empty string.

But if I save output from grep to a variable before calling wc -c, I get a different value:

$ match=$(echo "abc" | grep "j")
$ echo "$match" | wc -c
       1

The output from grep now is a string with 1 character in it.

I suspect that it's a newline in there:

$ echo $match

$

Why is there now an extra character in $match, and how can I keep that from happening?

答案1

得分: 2

match不包含换行符,但echo "$match"会写入一个换行符(但请参考下面的注释)。在第一种情况下,您直接将grep的输出传递给wc,但在第二种情况下,您将grep的输出和一个换行符一起传递给wc

但最好根本不这样做。无需引入wc来解决这个问题。只需测试grep返回的值。例如:

if ... | grep -q "$pattern"; then echo "$pattern在输入中找到了"; fi

注意,echo "$match"是不良实践。例如,考虑如果$match扩展为字符串-e-n。使用printf '%s' "$match"要更加健壮。

英文:

match does not contain a newline, but echo "$match" writes a newline (but see note below). In the first case, you are directly passing the output of grep to wc, but in the second case you are passing the output of grep plus a newline to wc.

But don't do this at all. There is no need to introduce wc into the problem. Just test the value returned by grep. eg:

if ... | grep -q "$pattern"; then echo "$pattern was found in the input"; fi

Note, echo "$match" is bad practice. For example, consider if $match expands to the string -e or -n. It is much more robust to use printf '%s' "$match"

答案2

得分: 1

请注意以下内容:

$ echo "$match" | od -c
0000000  \n
0000001
$ echo "$match" | wc -c
1

$ printf "$match" | od -c
0000000
$ printf "$match" | wc -c
0

这两个命令的唯一区别是echo在输出的末尾附加了\n

当然,我们也可以通过在printf中明确添加\n来生成相同的结果:

$ printf "$match\n" | od -c
0000000  \n
0000001

$ printf "$match\n" | wc -c
1
英文:

Consider the following:

$ echo "$match" | od -c
0000000  \n
0000001
$ echo "$match" | wc -c
1

$ printf "$match" | od -c
0000000
$ printf "$match" | wc -c
0

The only difference in the 2 commands is that echo appends a \n on the end of the ouput.

Of course, we can make the printf generate the same result by explicitly adding a \n on the end:

$ printf "$match\n" | od -c
0000000  \n
0000001

$ printf "$match\n" | wc -c
1

huangapple
  • 本文由 发表于 2023年2月10日 03:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定