在curl输出的每一行前添加前缀。

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

BASH: add prefix to each line on curl output

问题

我正在尝试找出如何在每行的开头添加一个前缀,其中这些行是curl命令的输出:

我有以下命令和输出:

curl -s https://www.url.com/ | grep -Eo '/news/[^ >]+'
/news/category/title-new-bla-bla
/news/category2/title-new-bla-bla2
/news/category3/title-new-bla-bla3
/news/category4/title-new-bla-bla4

我想要在我的命令中添加前缀 https://www.url.com/ 到每一行,可能使用sed或另一个工具,结果如下:

https://www.url.com/news/category/title-new-bla-bla
https://www.url.com/news/category2/title-new-bla-bla2
https://www.url.com/news/category3/title-new-bla-bla3
https://www.url.com/news/category4/title-new-bla-bla4

任何帮助将是非常棒的,请!

英文:

I'm trying to figure how to add a prefix at the beggining of each line where the lines are the output of curl:

I have the following command and output:

curl -s https://www.url.com/ | grep -Eo '/news/[^ >]+'

/news/category/title-new-bla-bla
/news/category2/title-new-bla-bla2
/news/category3/title-new-bla-bla3
/news/category4/title-new-bla-bla4

I would to add to my command, maybe using sed or another tool, the prefix https://www.url.com/ to each line as follows:

https://www.url.com/news/category/title-new-bla-bla
https://www.url.com/news/category2/title-new-bla-bla2
https://www.url.com/news/category3/title-new-bla-bla3
https://www.url.com/news/category4/title-new-bla-bla4

Any help would me amazing please!

答案1

得分: 2

以下是一个简单的解决方案:

  curl -s https://www.url.com/ \
| grep -Eo '/news/[^ >]+' \
| xargs -I "{}" echo "https://www.url.com{}"'
英文:

Here is a simple solution:

  curl -s https://www.url.com/ \
| grep -Eo '/news/[^ >]+' \
| xargs -I "{}" echo "https://www.url.com{}"'

答案2

得分: 2

使用 `sed`
```shell
$ sed -En '\~^/news/~s~^~https://www.url.com~p' <(curl -s https://www.url.com/)
https://www.url.com/news/category/title-new-bla-bla
https://www.url.com/news/category2/title-new-bla-bla2
https://www.url.com/news/category3/title-new-bla-bla3
https://www.url.com/news/category4/title-new-bla-bla4
英文:

Using sed

$ sed -En &#39;\~^/news/~s~^~https://www.url.com~p&#39; &lt;(curl -s https://www.url.com/)
https://www.url.com/news/category/title-new-bla-bla
https://www.url.com/news/category2/title-new-bla-bla2
https://www.url.com/news/category3/title-new-bla-bla3
https://www.url.com/news/category4/title-new-bla-bla4

</details>



# 答案3
**得分**: 1

你可以使用 `sed` 而不是 `grep`:

```sh
curl -s https://www.url.com/ | 
sed -nE 's~.*(/news/[^ &gt;]+).*~https://www.url.com~p'

这个 sed 命令会在每一行中查找相同的模式 /news/[^ &gt;]+,然后将每一行替换为以 https://www.url.com 为前缀的内容。

英文:

You can use sed instead of grep:

curl -s https://www.url.com/ | 
sed -nE &#39;s~.*(/news/[^ &gt;]+).*~https://www.url.com~p&#39;

This sed will search for the same pattern /news/[^ &gt;]+ in a line and in replacement it prefixes each line with https://www.url.com.

答案4

得分: 0

我们有:

curl -s https://www.url.com | grep -Eo '/news/[^ >]+'

然后,我们可以添加一个sed:

curl -s https://www.url.com | grep -Eo '/news[^ >]+' | sed 's|^|https://www.url.com/|'
英文:

we have:

curl -s https://www.url.com | grep -Eo &#39;/news/[^ &gt;]+&#39;

then, we can add a sed:

curl -s https://www.url.com | grep -Eo &#39;/news[^ &gt;]+&#39; \
| sed &#39;s|^|https://www.url.com/|&#39;

答案5

得分: 0

由于您正在使用GNU `grep`,我假设您也有GNU `awk`,在这种情况下,您可以使用多字符的`RS`和`RT`:

    curl -s https://www.url.com/ |
    awk -v RS='&#39;/news/[^ &gt;]+' 'RT{print "https://www.url.com/" RT}'
英文:

Since you are using GNU grep I assume you also have GNU awk in which case you can use multi-char RS and RT:

curl -s https://www.url.com/ |
awk -v RS=&#39;/news/[^ &gt;]+&#39; &#39;RT{print &quot;https://www.url.com/&quot; RT}&#39;

huangapple
  • 本文由 发表于 2023年7月13日 19:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679026.html
匿名

发表评论

匿名网友

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

确定