Re: Delete the path

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 08/31/04


Date: 31 Aug 2004 09:39:20 GMT

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

    Or you could use a shell loop:

while IFS= read -r line
do
   printf "%s\n" "${line##*/}
done < File.txt

    Or a filter in C ....... [OT - available on request]

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: redirect to start of file
    ... but this eliminates two external commands from the patch version ... ## COPYRIGHT 2007, Chris F.A. Johnson ... Released under Version 2 of the GNU General Public License ...
    (comp.os.linux.misc)
  • Re: Automagically setting the group when creating a file
    ... chmod g+s directory ... My code in this post is copyright 2003, Chris F.A. Johnson ... and may be copied under the terms of the GNU General Public License ...
    (comp.unix.questions)
  • Re: rename files
    ... 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.unix.shell)
  • Re: bash
    ... printf "%s\n" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ... My code in this post is copyright 2003, Chris F.A. Johnson ... and may be copied under the terms of the GNU General Public License ...
    (comp.unix.shell)
  • Re: how to find files containing certain word?
    ... man grep ... 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)