1

probably simple, but my head hurts.

i have a list of files with their path in a file named stuff.txt. Looks like this:

c:\users\none\file1.txt  
c:\users\none\file2.txt  
g:\home\share\make.log 

how can I simply get the file name output to another file to look like:

file1.txt  
file2.txt  
make.log

Thank you ahead of time.

6
  • 1
    basename Commented Jul 17, 2023 at 18:13
  • i didnt think basename works with files though.
    – Scott
    Commented Jul 17, 2023 at 18:27
  • Iterate the lines of the file. Fix the slashes. Then, basename it. Commented Jul 17, 2023 at 20:37
  • sed 's-.*\\__' should do the trick. Double-backslash as escaped backslash.
    – Philippos
    Commented Jul 18, 2023 at 5:19
  • 3
    Does this answer your question? Extract file name from path in awk program. Just replace the slash by backslashes
    – Philippos
    Commented Jul 18, 2023 at 10:55

2 Answers 2

2
awk -F'\' '{ print $NF }' stuff.txt

This sets the field separator to \ and prints the last field from each line

0
1

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put IO::Spec::Win32.basename($_);'   stuff.txt

#OR

~$ raku -ne 'put IO::Spec::Win32.basename($_.trim-trailing);'   stuff.txt

Raku is a programming language in the Perl-family. The code above takes your file paths and treats them as IO objects, of which there are four main classes (Unix, Windows, Cygwin, and QNX). Because you have Windows paths, the IO::Spec::Win32 method is called (to correctly understand the path delimiter). Finally basename gives you the file name. There seems to be some whitespace at the end of the lines, which can be cleaned up with trim or trim-trailing.

Sample Input:

c:\users\none\file1.txt  
c:\users\none\file2.txt  
g:\home\share\make.log

Sample Output:

file1.txt
file2.txt
make.log

https://docs.raku.org/type/IO/Spec/Win32
https://docs.raku.org/routine/basename
https://raku.org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .