Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Delete characters from the last "/"

Shell Programming and Scripting


Tags
delete, last character, sed

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 10-24-2012
msabhi's Unix or Linux Image
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 Unix and Linux 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 Unix and Linux 10-24-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 25 June 2015, 6:26 PM EDT
Location: Houston, Texas, USA
Posts: 682
Thanks: 0
Thanked 206 Times in 198 Posts
also:

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

or

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

Sponsored Links
    #5  
Old Unix and Linux 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 Unix and Linux 10-25-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 25 June 2015, 3:24 AM EDT
Posts: 3,060
Thanks: 34
Thanked 661 Times in 640 Posts

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

Sponsored Links
    #7  
Old Unix and Linux 10-25-2012
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 7:53 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:39 AM.