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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
Scott
  • 11

2 Answers2

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

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

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
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

jubilatious1
  • 3,195
  • 8
  • 17