LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   removing path prefix from filesnames (https://www.linuxquestions.org/questions/linux-general-1/removing-path-prefix-from-filesnames-885427/)

AndrewJS 06-09-2011 10:04 AM

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>

tredegar 06-09-2011 10:51 AM

Try this
Code:

for LINE in  $(cat textfile); do basename $LINE; done

arizonagroovejet 06-09-2011 01:18 PM

That won't work properly if any of the lines contain spaces.

Code:

while read LINE ;do basename "$LINE"; done < textfile

catkin 06-09-2011 01:39 PM

How about
Code:

sed 's|.*/||' filenames.txt

arizonagroovejet 06-09-2011 01:59 PM

Quote:

Originally Posted by catkin (Post 4381222)
How about
Code:

sed 's|.*/||' filenames.txt

Heh. My initial thought was that won't work if there's more than one / in a line. But then I tried it and it does work. Which puzzled me until I spent a few minutes on Google. This is a demonstration of how regular expressions are greedy by default isn't it?

catkin 06-09-2011 11:21 PM

Quote:

Originally Posted by arizonagroovejet (Post 4381241)
This is a demonstration of how regular expressions are greedy by default isn't it?

It is. For a bit of fun, what would be the regex to remove only up to the first "/"?

arizonagroovejet 06-10-2011 02:51 AM

Quote:

Originally Posted by catkin (Post 4381555)
It is. For a bit of fun, what would be the regex to remove only up to the first "/"?

Is this a trick question? / is the first character on the line so removing only up to the first / would be the same as removing nothing at all.

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
A/path/to/file2

? makes the expression non-greedy, so I was thinking
Code:

sed 's|.*?/||' filenames.txt
But that doesn't actually have any effect at all. This
Code:

sed -r 's|.*?/||' filenames.txt
does the same as if the ? wasn't in there.

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 :)

catkin 06-10-2011 10:18 AM

Quote:

Originally Posted by arizonagroovejet (Post 4381625)
Is this a trick question? / is the first character on the line so removing only up to the first / would be the same as removing nothing at all.

No -- it was a mistake :redface:
Quote:

Originally Posted by arizonagroovejet (Post 4381625)
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 :)

That solves the challenge I meant to set :)


All times are GMT -5. The time now is 08:25 AM.