Delete characters from the last "/" | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Delete characters from the last "/"

Shell Programming and Scripting


Tags
delete, last character, sed

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-24-2012
inaki inaki is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 7 August 2014, 5:22 AM EDT
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
Delete characters from the last "/"

Hi,

I have a text file with some lines like this:

Code:
/MEDIA/DISK1/23568742.MOV 
/MEDIA/DISK1/87456321.AVI 
/MEDIA/DISK2/PART1/45753131.AVI 
/IMPORT/44452.WAV 
...

I don't konw how to delete all characters from the last "/" and the text file looks like this:


Code:
/MEDIA/DISK1/
/MEDIA/DISK1/
/MEDIA/DISK2/PART1/
/IMPORT/
...

Thank you.

Iñaki
Sponsored Links
    #2  
Old 10-24-2012
msabhi's Avatar
msabhi msabhi is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 2 November 2013, 1:26 AM EDT
Location: In books/UNIX.com
Posts: 177
Thanks: 16
Thanked 61 Times in 60 Posts

Code:
sed 's@\(.*/\).*$@\1@' input_file

The Following User Says Thank You to msabhi For This Useful Post:
inaki (10-24-2012)
Sponsored Links
    #3  
Old 10-24-2012
inaki inaki is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 7 August 2014, 5:22 AM EDT
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
Than you Msabhi it works perfectly.
    #4  
Old 10-24-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
also:

Code:
sed 's/[^/]*$//' infile

or

Code:
awk -F/ '{$NF=""}1' OFS=/ infile

Sponsored Links
    #5  
Old 10-24-2012
scottaazz scottaazz is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 6 November 2012, 8:00 PM EST
Posts: 28
Thanks: 0
Thanked 7 Times in 7 Posts
Just in case your next question is going to be how to get the file name or other info. Perl has a good way of parsing (that is independent of operating systems):


Code:
 perl -ane 'BEGIN { use File::Basename; } {($n,$d,$t)=fileparse(@F); print $d,"\n"}' yourfile
 
basically 
$n  is your filename
$d directory
$t is the type

Sponsored Links
    #6  
Old 10-25-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 657 Times in 636 Posts

Code:
$ perl -F/ -lane 'pop @F;print join("/",@F),"/";' input.txt
/MEDIA/DISK1/
/MEDIA/DISK1/
/MEDIA/DISK2/PART1/
/IMPORT/

Sponsored Links
    #7  
Old 10-25-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts

Code:
perl -pe 's:(.*/).*:$1:' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete characters from each line until meet character ":" black_fender Shell Programming and Scripting 8 05-18-2012 02:41 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Delete files older than "x" if directory size is greater than "y" JamesCarter Shell Programming and Scripting 4 02-11-2010 07:41 AM
Unix commands delete all files starting with "X" except "X" itself. HELP!!!!? soccerball Shell Programming and Scripting 2 10-15-2009 07:24 PM
How to remove "New line characters" and "spaces" at a time anushree.a Shell Programming and Scripting 4 09-08-2008 05:55 PM



All times are GMT -4. The time now is 06:07 AM.