简体   繁体   English

试图在 sed 中打印每一行的一部分

[英]Trying to print a part of each line in sed

I'm trying to use sed to print just the last part of the url from a list of data.我正在尝试使用 sed 从数据列表中仅打印 url 的最后一部分。 I want it to be using sed not awk.我希望它使用 sed 而不是 awk。

Input data is like this:输入数据是这样的:

Place,AF,http://en.wikipedia.org/wiki/Benin
Place Mat,NA,http://en.wikipedia.org/wiki/Saint_Barthelemy
Orion,NA,http://en.wikipedia.org/wiki/Bermuda

I want to just print the last part of the URL like this (WANT THIS):我只想像这样打印 URL 的最后一部分(想要这个):

Benin
Saint Barthelemy
Bermuda

I'm having so much trouble with the / and \\ because they exist in the url!!!我在 / 和 \\ 上遇到了很多麻烦,因为它们存在于 url 中!!!

My attempt so far (tryng to replace stuff I don't want with nothing)到目前为止我的尝试(试图用空替换我不想要的东西)

sed -r s/$.+wikipedia\.org\/// in.txt

Also I need to replace spaces with _ but I can use the y command y/_/ / I think?另外我需要用 _ 替换空格,但我可以使用 y 命令 y/_/ / 我想?

With GNU sed:使用 GNU sed:

sed 's/.*\///;s/_/ /g' file

or replace first s/// by s||||或将第一个s///替换为s|||| to avoid escaping:避免逃跑:

sed 's|.*/||;s/_/ /g' file

Output:输出:

Benin
Saint Barthelemy
Bermuda

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM