Extract filename from file using shell script

0

I have a text file which has the following line:

/u/tux/abc/spool/frtbla_mcdetc_0000149099_20101126135009990_1.tif

I want to extract frtbla_mcdetc_0000149099_20101126135009990_1.tif; the word after the last slash (/).

Grv

Posted 2015-07-06T12:07:18.137

Reputation: 5

if you are open to using excel - you easily accomplish this - "without VB script" – Prasanna – 2015-07-06T12:12:23.497

Answers

1

If you are in a Linux-like environment you can use the basename utility:

basename $(<your_file)

cYrus

Posted 2015-07-06T12:07:18.137

Reputation: 18 102

thanks.. i came up with the method..firstly i removed all "/" than used the last parameter..but your's is the best.. – Grv – 2015-07-06T12:57:06.360

0

use this command

cat text_file_name | cut -d '/' -f 6

psycho3

Posted 2015-07-06T12:07:18.137

Reputation: 127

Looks like we both answered at the same time! Just for your own edification, cut operates on files directly. Often simply cating a file falls under what's known - somewhat judgementally - as a "useless use of cat"; unless you are viewing the file or similar (though there again you could use less)

– bertieb – 2015-07-06T12:20:26.980

@bertieb appreciated – psycho3 – 2015-07-06T12:30:41.940

0

If you know the exact format of the directory structure and it won't alter, you can use cut:

$ cut -f6 -d '/' file.txt

Here uses cut to treat the directory separators as a delimiter and extract the 6th one.

If instead all you know is it is the last part of a line but don't know the directory structure, you can use rev as well:

$ rev file.txt | cut -f1 -d '/' | rev

Here the file is reversed and the first field is extracted, before being reversed again.

bertieb

Posted 2015-07-06T12:07:18.137

Reputation: 6 181

0

The following applies to all strings in a shell, not just filenames, and is far easier than cut because you don't need to know how many fields there are before the one you want:

$ foo=/path/to/file/split/by/slashes.txt
$ echo ${foo##*/}
slashes.txt

This uses the 'greedy trim', i.e. trim everything until the last '/' as described here:

https://stackoverflow.com/questions/3162385/how-to-split-a-string-in-shell-and-get-the-last-field

${foo  <-- from variable foo
  ##   <-- greedy front trim
  *    <-- matches anything
  /    <-- until the last '/'
 }

qasdfdsaq

Posted 2015-07-06T12:07:18.137

Reputation: 5 762

0

I don't know what the standard shell is in Aix, but bash is available and supports edited expansion of variables.

If your full name is in the variable FileName, then ${FileName##*/} displays the name with all leading characters deleted as far as the last /; by contrast ${FileName#*/} deletes up to the first /, while ${FileName%/*} deletes trailing characters from the last / (ie the directory path).

If you generate the file name(s) by a find command, then you need a command like:

find ... | while read FileName; do echo ${FileName##*/}; done

If in a text file:

while read FileName; do echo ${FileName##*/}; done < FileList.txt

Replace the echo command by whatever processing you need to do with the name.

AFH

Posted 2015-07-06T12:07:18.137

Reputation: 15 470

(I seem to have crossed with other answers, but I'll let my answer stand, as parts of it are not covered elsewhere.) – AFH – 2015-07-06T13:28:47.863