【问题标题】:Curl the results of a grep?卷曲 grep 的结果?
【发布时间】:2018-06-07 13:18:49
【问题描述】:

这是我正在寻找线条的方式:

grep "random text" /

我想根据找到的文本进行卷曲。这是我尝试过的:

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

我不明白如何在卷曲时使用 grep 的结果。如何替换我的 grep 结果的 [TEXT HERE] 以便获得正确的 url?

【问题讨论】:

  • grep: /: Is a directory
  • 迄今为止提出的解决方案对您想要什么做出了不同的假设,因为您并不清楚。您希望每行 grep 结果有一个 curl 命令,还是要将整个 grep 结果发送到单个 curl 命令?

标签: linux bash shell curl grep


【解决方案1】:

在一个请求中传递来自 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

【讨论】:

  • 文本可能需要进行 url 编码。也不清楚他是想对每个匹配的行做一个单独的请求,还是作为一个请求全部发送。
【解决方案2】:
grep 'random text' file | xargs -I {} curl 'http://example.com/test.php?text={}'

【讨论】:

    【解决方案3】:

    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? 了解执行此操作的各种方法。

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2013-07-27
      • 2023-04-08
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多