removing path prefix from filesnames
I have a text file containing file names with their paths prefixed as in:
/path1/file1.dat /path2/file2.dat and so on. I want to remove the path prefix from all the file names and end up with just the file name itself, as in: file1.dat file2.dat what would be the best (easiest) way to do this?....I don't think basename would be easy to implement as it only takes on operand at a time...is there any task similar to base name so that I could just pipe the contents of my text file containing the file names ino it? as in: textfile | <pathremovingtask> |
Try this
Code:
for LINE in $(cat textfile); do basename $LINE; done |
That won't work properly if any of the lines contain spaces.
Code:
while read LINE ;do basename "$LINE"; done < textfile |
How about
Code:
sed 's|.*/||' filenames.txt |
Quote:
|
Quote:
|
Quote:
I had a play with it anyway using a list of files names where I put A at the start of each line Code:
A/path/to/file1 Code:
sed 's|.*?/||' filenames.txt Code:
sed -r 's|.*?/||' filenames.txt A few minutes on Google reveals that sed doesn't actually support non-greed regular expressions. So I think you have to do it by matching any number of characters which aren't a / Code:
$ sed 's|[^/]*||' filenames.txt Regular expressions are great. I've been using them for years and still every now and then I encounter situations where they don't work like I expect them to :) |
Quote:
Quote:
|
All times are GMT -5. The time now is 08:25 AM. |