LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-09-2011, 11:04 AM   #1
AndrewJS
LQ Newbie
 
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
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>
 
Old 06-09-2011, 11:51 AM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,038

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Try this
Code:
for LINE in  $(cat textfile); do basename $LINE; done
 
Old 06-09-2011, 02:18 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
That won't work properly if any of the lines contain spaces.

Code:
while read LINE ;do basename "$LINE"; done < textfile
 
Old 06-09-2011, 02:39 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
How about
Code:
sed 's|.*/||' filenames.txt
 
Old 06-09-2011, 02:59 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
Quote:
Originally Posted by catkin View Post
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?
 
Old 06-10-2011, 12:21 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by arizonagroovejet View Post
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 "/"?
 
Old 06-10-2011, 03:51 AM   #7
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
Quote:
Originally Posted by catkin View Post
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
 
Old 06-10-2011, 11:18 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by arizonagroovejet View Post
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
Quote:
Originally Posted by arizonagroovejet View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference b/w AF_ prefix and PF_ prefix related to socket Ashok_mittal Linux - Newbie 1 03-20-2008 05:15 PM
Building .deb with customized prefix path SamTim74 Debian 0 01-22-2008 08:08 PM
Default prefix path when installing from source jrdioko Linux - Newbie 5 01-05-2006 10:40 PM
Removing path form PATH maginotjr Linux - Newbie 3 02-12-2005 03:23 PM
removing one directory in PATH env chunlee Linux - Newbie 4 11-06-2004 08:50 PM


All times are GMT -5. The time now is 02:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration