英文:
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 '\~^/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
</details>
# 答案3
**得分**: 1
你可以使用 `sed` 而不是 `grep`:
```sh
curl -s https://www.url.com/ |
sed -nE 's~.*(/news/[^ >]+).*~https://www.url.com~p'
这个 sed
命令会在每一行中查找相同的模式 /news/[^ >]+
,然后将每一行替换为以 https://www.url.com
为前缀的内容。
英文:
You can use sed
instead of grep
:
curl -s https://www.url.com/ |
sed -nE 's~.*(/news/[^ >]+).*~https://www.url.com~p'
This sed
will search for the same pattern /news/[^ >]+
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 '/news/[^ >]+'
then, we can add a sed:
curl -s https://www.url.com | grep -Eo '/news[^ >]+' \
| sed 's|^|https://www.url.com/|'
答案5
得分: 0
由于您正在使用GNU `grep`,我假设您也有GNU `awk`,在这种情况下,您可以使用多字符的`RS`和`RT`:
curl -s https://www.url.com/ |
awk -v RS=''/news/[^ >]+' '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='/news/[^ >]+' 'RT{print "https://www.url.com/" RT}'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论