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: 16 December 2014, 11:19 AM EST
Location: Houston, Texas, USA
Posts: 676
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: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 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  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 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 03:41 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 09:59 AM
Delete files older than "x" if directory size is greater than "y" JamesCarter Shell Programming and Scripting 4 02-11-2010 08:41 AM
Unix commands delete all files starting with "X" except "X" itself. HELP!!!!? soccerball Shell Programming and Scripting 2 10-15-2009 08:24 PM
How to remove "New line characters" and "spaces" at a time anushree.a Shell Programming and Scripting 4 09-08-2008 06:55 PM



All times are GMT -4. The time now is 11:39 PM.