Deleting all characters before the last occurrence of /

Hi All,

I have a text file with the following text in it:

file:///About/accessibility.html
file:///About/disclaimer.html
file:///About/disclaimer.html#disclaimer
file:///pubmed?term=%22Dacre%20I%22%5BAuthor%5D
file:///pubmed?term=%22Madigan%20J%22%5BAuthor%5D
http://www.facebook.com/ncbi.nlm
http://www.nlm.nih.gov/privacy.html

I want to delete all the text that occurs before the last /. This means, my output should look like this:

accessibility.html
disclaimer.html
disclaimer.html#disclaimer
pubmed?term=%22Dacre%20I%22%5BAuthor%5D
pubmed?term=%22Madigan%20J%22%5BAuthor%5D
ncbi.nlm
privacy.html

I tried this using two different commands after searching this forum but I think I am making some mistakes:
These are the commands that I issued:

perl -p -e 's/^.*?//' 1.txt

and

sed 's/^.*/\/\//' 1.lin

I am using Linux with Bash.

perl -pe 's!.*/!!' 1.txt
1 Like

HI,
Seems its not working. I am getting this error message:

.: Event not found.

I am trying different ways of solving this now. If I can solve this then I'll post my code.

I think you used double quotes (") instead of single ('). Anyway you can use this code with double quotes:

perl -pe "s/.*\///" file
1 Like

Great it worked.!!!

awk -F/ '$0=$NF' file
1 Like

Using shell builtin properties

echo "file:///About/accessibility.html
file:///About/disclaimer.html
file:///About/disclaimer.html#disclaimer
file:///pubmed?term=%22Dacre%20I%22%5BAuthor%5D
file:///pubmed?term=%22Madigan%20J%22%5BAuthor%5D
http://www.facebook.com/ncbi.nlm
http://www.nlm.nih.gov/privacy.html"  | while read filename
do
        echo "${filename##*/}"
done
1 Like
while read line; do basename $line; done < infile
1 Like

By sed..

sed 's/.*\///' inputfile
1 Like