Linux 将grep传递给bash中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10992814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing grep into a variable in bash
提问by newbie.my
I have a file named email.txt like these one :
我有一个名为 email.txt 的文件,如下所示:
Subject:My test
From:my email <[email protected]>
this is third test
I want to take out only the email address in this file by using bash script.So i put this script in my bash script named myscript:
我想通过使用 bash 脚本只取出这个文件中的电子邮件地址。所以我把这个脚本放在名为 myscript 的 bash 脚本中:
#!/bin/bash
file=$(myscript)
var1=$(awk 'NR==2' $file)
var2=$("$var1" | (grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'))
echo $var2
But I failed to run this script.When I run this command manually in bash i can obtain the email address:
但是我无法运行此脚本。当我在 bash 中手动运行此命令时,我可以获得电子邮件地址:
echo $var1 | grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'
I need to put the email address to store in a variable so i can use it in other function.Can someone show me how to solve this problem? Thanks.
我需要将电子邮件地址存储在一个变量中,以便我可以在其他函数中使用它。有人可以告诉我如何解决这个问题吗?谢谢。
采纳答案by flesk
I think this is an overly complicated way to go about things, but if you just want to get your script to work, try this:
我认为这是一种过于复杂的处理方式,但是如果您只是想让脚本正常工作,请尝试以下操作:
#!/bin/bash
file="email.txt"
var1=$(awk 'NR==2' $file)
var2=$(echo "$var1" | grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b')
echo $var2
I'm not sure what file=$(myscript)
was supposed to do, but on the next line you want a file name as argument to awk
, so you should just assign email.txt
as a string value to file
, not execute a command called myscript
. $var1
isn't a command (it's just a line from your text file), so you have to echo
it to give grep
anything useful to work with. The additional parentheses around grep
are redundant.
我不确定file=$(myscript)
应该做什么,但在下一行,您想要一个文件名作为 的参数awk
,因此您应该只将email.txt
字符串值分配给file
,而不是执行名为myscript
. $var1
不是命令(它只是文本文件中的一行),因此您必须使用echo
它来提供grep
任何有用的东西。周围的附加括号grep
是多余的。
回答by puckipedia
What is happening is this:
正在发生的事情是这样的:
var2=$("$var1" | (grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'))
^^^^^^^ Execute the program named (what is in variable var1).
You need to do something like this:
你需要做这样的事情:
var2=$(echo "$var1" | grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b')
or even
甚至
var2=$(awk 'NR==2' $file | grep -Eio '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b')
回答by gshilin
There are very helpful flags for bash: -xv
bash 有一些非常有用的标志:-xv
The line with
线与
var2=$("$var1" | (grep...
should be
应该
var2=$(echo "$var1" | (grep...
Also my version of grep doesn't have -o flag.
我的 grep 版本也没有 -o 标志。
And, as far as grep patterns are "greedy" even as the following code runs, it's output is not exactly what you want.
而且,就 grep 模式是“贪婪的”而言,即使下面的代码运行,它的输出也不是你想要的。
#!/bin/bash -xv
file=test.txt
var1=$(awk 'NR==2' $file)
var2=$(echo "$var1" | (grep -Ei '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b'))
echo $var2
回答by Prince John Wesley
Use Bash parameter expansion
,
使用Bash parameter expansion
,
var2="${var1#*:}"
回答by Peter Flynn
There's a cruder way:
有一个更粗暴的方法:
cat $file | grep @ | tr '<>' '\012\012' | grep @
猫 $file | 格雷普@ | tr '<>' '\012\012' | 格雷普@
That is, extract the line[s] with @ signs, turn the angle brackets into newlines, then grep again for anything left with an @ sign.
也就是说,提取带有 @ 符号的行 [s],将尖括号变成换行符,然后再次 grep 查找带有 @ 符号的任何剩余内容。
Refine as needed...
根据需要细化...