多个字符串在curl -s之后使用Grep筛选。

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

Grep multiple strings after curl -s

问题

以下是翻译好的部分:

我正在尝试编写一个Bash脚本,其中我提供了一些变量,如下所示:

var1="text1"
var2="text2"
var3="text3"
var4="text4"
var5="text5"
var6="text6"

我已经尝试了一个变量,并且以下脚本可以正常工作:
if curl -s "https://web.net/list.txt" | grep $var1
echo $var1 is on the list
else
echo "FFHGFHFGHFHFG"
fi

我一直在尝试输入其他变量,因为这些是硬编码的,不会更改,就像这样:
if curl -s "https://web.net/list.txt" | grep "$var1\|$var2"

if curl -s "https://web.net/list.txt" | grep '$var1|$var2'

我记不清我尝试过的所有方法,但这是我目前的尝试。

想法是在给定的网站中查找这些值,如果存在则输出这些值,否则不输出任何内容。
英文:

I'm trying to write a bash script where I give a few variables as such:

var1="text1"
var2="text2"
var3="text3"
var4="text4"
var5="text5"
var6="text6"

I've tried it with one variable and the following script works fine:

if curl -s "https://web.net/list.txt" | grep $var1
echo $var1 is on the list
else
echo "FFHGFHFGHFHFG"
fi

I've been trying to input the other variables, which are hardcoded as those are not going to change, like this:

if curl -s "https://web.net/list.txt" | grep "$var1\|$var2"

if curl -s "https://web.net/list.txt" | grep '$var1|$var2'

I cannot remember all of the ways I've tried to do this, but that is my current attempt.

The idea is to find those values in a given site, output those values if the exist and nothing else.

答案1

得分: 1

这部分的翻译如下:

这个操作成功:

var1=head;var2=body;curl -s http://free.fr | grep "$var1\|$var2"

最后变成了这样:

var1=something1;var2=something2;var3=something3;var4=something4;var5=something5;var6=something6;

代码运行:

var1=something1;var2=something2;var3=something3;var4=something4;var5=something5;var6=something6;
if curl -s https://website.net/text.txt | grep "$var1\|$var2\|$var3\|$var4\|$var5\|$var6"
then
echo $var1 
echo $var2
else (...)

之后,我将结果发送到文件以供进一步分析。

谢谢大家。

英文:

This worked:

var1=head;var2=body;curl -s http://free.fr | grep "$var1\|$var2"

Ended up doing this:

var1=something1;var2=something2;var3=something3;var4=something4;var5=something5;var6=something6;

Code runs

var1=something1;var2=something2;var3=something3;var4=something4;var5=something5;var6=something6;
if curl -s https://website.net/text.txt | grep "$var1\|$var2\|$var3\|$var4\|$var5\|$var6" 
then
echo $var1 
echo $var2
else (...)

and so on.

After that I'm sending the results to a file for further analysis.

Thanks all.

答案2

得分: 1

您可以向 grep 提供多个模式,如果其中任何一个匹配成功,它都会返回成功:

if curl -s 'https://web.net/list.txt' | grep -e "$var1" -e "$var2" -e "$var3" -e "etc."; then
  echo "包含 $var1$var2$var3 或其他...";
fi

这样做还允许您指定 -F 以仅匹配固定字符串。这有助于防止变量包含元字符如 *. 时出现问题。

英文:

You can provide multiple patterns to grep and it will return successfully if any of them matches:

if curl -s 'https://web.net/list.txt' | grep -e "$var1" -e "$var2" -e "$var3" -e "etc."; then
  echo "Contains $var1 or $var2 or $var3 or ..."
fi

Doing it this way also allows you to specify -F to match only on fixed strings. This helps to prevent issues if your variables contain meta characters such as * or ..

huangapple
  • 本文由 发表于 2023年8月4日 01:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830232.html
匿名

发表评论

匿名网友

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

确定