Извлечь имя файла из файла, используя скрипт

1645
Grv

У меня есть текстовый файл, который имеет следующую строку:

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

Я хочу извлечь frtbla_mcdetc_0000149099_20101126135009990_1.tif; слово после последней косой черты ( /).

0
если вы открыты для использования Excel - вы легко это делаете - "без VB-скрипта" Prasanna 9 лет назад 0

5 ответов на вопрос

1
cYrus

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

basename $(<your_file) 
спасибо .. я придумал метод .. во-первых, я удалил все "/", чем использовал последний параметр .. но ваш лучший ... Grv 9 лет назад 0
0
psycho3

use this command

cat text_file_name | cut -d '/' -f 6 
Похоже, мы оба ответили одновременно! Просто для вашего удобства, cut работает с файлами напрямую. Часто просто `cat`ing 'файл подпадает под то, что известно - в некотором смысле, по закону - как" [бесполезное использование `cat`] (http://www.smallo.ruhr.de/award.html)"; если вы не просматриваете файл или подобный файл (хотя и здесь вы можете использовать `less`) bertieb 9 лет назад 0
@bertieb оценил psycho3 9 лет назад 0
0
bertieb

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.

0
qasdfdsaq

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

$ 
0
AFH

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.

(Я, кажется, перешел с другими ответами, но я оставлю свой ответ в силе, поскольку его части не рассматриваются в другом месте.) AFH 9 лет назад 0

Похожие вопросы