简体   繁体   中英

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. I want it to be using sed not 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):

Benin
Saint Barthelemy
Bermuda

I'm having so much trouble with the / and \\ because they exist in the 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?

With GNU sed:

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

or replace first s/// by s|||| to avoid escaping:

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

Output:

Benin
Saint Barthelemy
Bermuda

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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