卷曲grep的结果?

时间:2017-12-26 19:33:31

标签: linux bash shell curl grep

以下是我如何使用线条:

grep "random text" /

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

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

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

3 个答案:

答案 0 :(得分:3)

在一个请求中传递来自grep的所有结果:

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

每个grep结果一个请求:

while循环与read结合使用:

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

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

如果$output可以包含特殊的网址字符,则需要对其进行网址编码。有关执行此操作的各种方法,请参阅How to urlencode data for curl command?