Wednesday, February 7, 2024

[SOLVED] How to find complete file names in UNIX if i know only extention of file.

Issue

Suppose I have a file which contains other file names with some extention [.dat,.sum etc].

text file containt

 gdsds sd8ef g/f/temp_temp.sum 
 ghfp hrwer h/y/test.text.dat
 if[-r h/y/somefile.dat] then....

I want to get the complete file names, like for above file I should get output as

temp_temp.sum
test.text.dat
somefile.dat

I am using AIX unix in which grep -ow [a-zA-Z_] filename is not working as for AIX -o switch is not present.


Solution

sed is good, but as you have a range of types of 'records', maybe awk can help.

My target is any 'word' found by awk that has a '/' in it, then take that word, remove everything up to the last '/', leaving just the filename.

{ 
   cat -<<EOS
   gdsds sd8ef g/f/temp_temp.sum 
   ghfp hrwer h/y/test.text.dat
   if[-r h/y/somefile.dat] then....
EOS
} \
| awk '{ 
          for (i=1; i<=NF;i++) {
            if ($i ~ /.*\//) {
              fName=$i
              sub(/.*\//, "", fName)
              # put any other chars you to to delete inside the '[ ... ]' char list
              sub(/[][]/, "", fName)
              if (fName) {
                 print file
              }
            }
         }
       }'

output

temp_temp.sum
test.text.dat
somefile.dat


Answered By - shellter
Answer Checked By - Pedro (WPSolving Volunteer)