If you are in a Linux-like environment you can use the basename
utility:
basename $(<your_file)
У меня есть текстовый файл, который имеет следующую строку:
/u/tux/abc/spool/frtbla_mcdetc_0000149099_20101126135009990_1.tif
Я хочу извлечь frtbla_mcdetc_0000149099_20101126135009990_1.tif
; слово после последней косой черты ( /
).
If you are in a Linux-like environment you can use the basename
utility:
basename $(<your_file)
use this command
cat text_file_name | cut -d '/' -f 6
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.
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 $ 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
$
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 $
displays the name with all leading characters deleted as far as the last /
; by contrast $
deletes up to the first /
, while $
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 $; done
If in a text file:
while read FileName; do echo $; done < FileList.txt
Replace the echo
command by whatever processing you need to do with the name.