Deleting all characters before the last occurrence of /


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting all characters before the last occurrence of /
# 1  
Old 07-09-2011
Deleting all characters before the last occurrence of /

Hi All,

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


Code:
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:

Code:
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:

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

and

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

I am using Linux with Bash.
# 2  
Old 07-09-2011
Code:
perl -pe 's!.*/!!' 1.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-09-2011
HI,
Seems its not working. I am getting this error message:

Code:
.: Event not found.

I am trying different ways of solving this now. If I can solve this then I'll post my code.
# 4  
Old 07-09-2011
I think you used double quotes (") instead of single ('). Anyway you can use this code with double quotes:
Code:
perl -pe "s/.*\///" file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-09-2011
Great it worked.!!!
# 6  
Old 07-09-2011
Code:
awk -F/ '$0=$NF' file

This User Gave Thanks to danmero For This Post:
# 7  
Old 07-09-2011
Using shell builtin properties
Code:
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

This User Gave Thanks to kshji For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting Specific Characters in Script

What's one single UNIX pipeline that would delete all of the vowels from one file (global)? I know I would need to use the sed command. But I'm stuck after that. i know how to replace characters with other characters I just don't know how to fully get rid of it. (4 Replies)
Discussion started by: sarahahah
4 Replies

2. UNIX for Dummies Questions & Answers

Split binary file every occurrence of a group of characters

Hello I am new to scripts, codes, bash, terminal, etc. I apologize this my be very scattered because I frankly don't have any idea where to begin and I have had trouble sleeping lately. I have several 2GB files I wish to split. This Code 00 00 01 BA ** ** ** ** ** ** ** ** C3 F8 00 00 01 BB 00... (17 Replies)
Discussion started by: PatrickE
17 Replies

3. Shell Programming and Scripting

Deleting new line characters

Hi, I have a weird requirement. I am having a file with 12fields in it and the end of the line for each record is "\n" (Just \n and no carriage returns) and the field delimiter is "|". Problem is I can have new line characters in any field in the data and these new line characters can even come... (11 Replies)
Discussion started by: ngkumar
11 Replies

4. UNIX for Dummies Questions & Answers

counting occurrence of characters in a string

Hello, I have a string like this 0:1:2:0:2:2:4:0:0:0:-200:500...... what i want is to break down how many different characters are there and their count. For example for above string it should display 0 - 5 times 1 - 1 times 2 - 3 times 4 - 1 times . . . I am stuck in writing... (8 Replies)
Discussion started by: exit86
8 Replies

5. Shell Programming and Scripting

deleting rows that have certain characters

Hi, I want to delete rows whenever column one has the letters 'rpa'. The file is tab seperated. e.g. years 1 bears 1 cats 2 rpat 3 rpa99 4 rpa011 5 then removing 'rpa' containing rows based on the first column years 1 bears 1 cats 2 thanks (7 Replies)
Discussion started by: phil_heath
7 Replies

6. Shell Programming and Scripting

Help need in Deleting Characters

Hi, I have a log file whose size is number of characters in the file with multiple lines. Example: SQL*Loader: Release 10.2.0.4.0 - Production on Sat Sep 12 07:55:29 2009 Copyright (c) 1982, 2007, Oracle. All rights reserved. Control File: ../adm/ctl/institution.ctl Character Set... (4 Replies)
Discussion started by: rajeshorpu
4 Replies

7. Shell Programming and Scripting

Deleting values with specific characters

I have a file with 3 columns 2 4 5 2 4 7 3 5 7 4 -6 9 5 -9 4 6 -3 3 Bascially I want to delete the entire row if column 2 is a "-" So the end result will be 2 4 5 2 4 7 3 5 7 I have trouble doing this cause of the - in front of the number. thanks (2 Replies)
Discussion started by: kylle345
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with deleting certain characters on a line

I have a file that looks like this: It is a huge file and basically I want to delete everything at the > line except for the number after “C”. >c1154... (2 Replies)
Discussion started by: kylle345
2 Replies

9. Shell Programming and Scripting

deleting last characters of a word

Hi All is there a way to delete last n characters from a word like say i have employee_new i want to delete _new. and just get only employee I want this in AIX Shell scripting Thanks (3 Replies)
Discussion started by: rajaryan4545
3 Replies

10. Shell Programming and Scripting

Deleting First Two Characters On Each Line

How would one go about deleting the first two characters on each line of a file on Unix? I thought about using awk, but cannot seem to find if it can explicitly do this. In this case there might or might not be a field separator. Meaning that the data might look like this. 01999999999... (5 Replies)
Discussion started by: scotbuff
5 Replies
Login or Register to Ask a Question