Re: Delete the path

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 08/31/04


Date: Tue, 31 Aug 2004 07:51:30 -0500


Chris F.A. Johnson wrote:

> On 2004-08-31, JuanL wrote:
>
>>Hi,
>>
>>I have a list of files in a txt file (ls -l *.txt > File.txt ) and I
>>want to cut their path and
>>save the new list into another file.
>>
>>File.txt:
>> /path1/xxx/tmp_001.txt
>> /path1/xxx/tmp_002.txt
>> /path1/xxx/tmp_003.txt
>>
>>I have tried the following:
>>
>>tr -d '/path1/xxx/' < File.txt > NewFile.txt
>>
>>The problem with that is that NewFile.txt will be:
>>tmp_00.txt <---- tr -d finds the '1' and replace it
>>tmp_002.txt
>>tmp_003.txt
>
>
> tr is for translating characters, not strings.
>
> This sed commands removes everything up to the final slash on each
> line:
>
> sed 's|.*/||' File.txt
>
> Or you could use awk:
>
> awk 'sub(/.*\//,"")' File.txt

IMHO, this would be the more typical way:

awk -F/ '{print $NF}' File.txt

> Or you could use a shell loop:
>
> while IFS= read -r line
> do
> printf "%s\n" "${line##*/}

or
        basename "$line"

> done < File.txt
>

You could just use:

        xargs basename < File.txt

if your file/dir names don't contain white-space.

Regards,

        Ed.



Relevant Pages

  • Re: Search for best matched portion of a string
    ... Chris F.A. Johnson wrote: ... >>The strings that I mentioned are just examples...may be the words can ... And please don't continue these separate threads in multiple NGs. ...
    (comp.unix.shell)
  • Re: finding a string in a file
    ... > I search for strings in dir-trees 4 times a day, ... My code in this post is copyright 2004, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License ...
    (comp.os.linux.misc)
  • Re: how to trim variable
    ... On 2005-10-05, Chris F.A. Johnson wrote: ... > I thought you knew bash. ... > Here Strings ...
    (comp.unix.shell)
  • Football for Baseball
    ... 193 Chris Simms ... 199 Rudi Johnson ... 225 Mike Peterson ... Marvin Harrison ...
    (rec.collecting.sport.football)
  • football for baseball
    ... Thing is we only collect cards of a few baseball players ... 193 Chris Simms ... 199 Rudi Johnson ... Marvin Harrison ...
    (rec.collecting.sport.baseball)