卷曲grep的结果?

问题描述 投票:0回答:3

这是我如何利用线条:

grep "random text" /

我想根据找到的文字卷曲。这就是我尝试过的:

grep "random text" / | curl http://example.com/test.php?text=[TEXT HERE]

我不明白怎么做是在卷曲时使用grep的结果。我如何更换我的grep结果的[TEXT HERE],以便获得正确的URL?

linux bash shell curl grep
3个回答
3
投票

Passing all results from grep in one request:

 curl --data-urlencode "text=$(grep PATTERN file)" "http://example.com/test.php"

One request per grep result:

while循环与read结合使用:

grep PATTERN file | while read -r value ; do
    curl --data-urlencode "text=${value}" "http://example.com/test.php"
done

1
投票
grep 'random text' file | xargs -I {} curl 'http://example.com/test.php?text={}'

0
投票

grep的输出放在变量中,并将该变量放在[TEXT HERE]的位置

output=$(grep "random text" filename)
curl "http://example.com/test.php?text=$output"

引号很重要,因为?对shell有特殊含义,而$output可能包含空格或通配符,否则将被处理。

如果$output可以包含特殊的URL字符,则需要对其进行URL编码。请参阅How to urlencode data for curl command?了解各种方法。

© www.soinside.com 2019 - 2024. All rights reserved.