Welcome to the Linux Foundation Forum!

using find command for multiple files and then manipulating the results

Hello. I am new here. I have Linux experience but it is fairly limited. I have been trying to find a solution to the following problem:

I have a directory that contains multiple files. First, I need to find all files with an extension of ".trg" I have successfully done this using the find command:

param=`find *${file_name}*.trg`

this provides me with two filenames in the param variable:

ABC1099C.trg ABC1099R.trg

I need to take the text prior to the .trg (so ABC1099 from the first occurrence) and then find the associated filename with a .dat extension.

I tried using string operators but was unsuccessful. Maybe I should be using some kind of array processing?

Ideas?

Any help would be greatly appreciated.

THANKS

Comments

  • mfillpot
    mfillpot Posts: 2,177
    how about something like:
    #!/bin/bash
    CWD="$(pwd)"
    for FIL in $(find . -name *.trg)
    do
      DAT=$(echo "$FIL" | sed 's/\.trg/\.dat')
      if [ -f $CWD/$DAT ]; then
         echo "$FIL and & $DAT exist"
      else
         echo "$DAT does not exist"
      fi
    done
    

    this loops through the results of the find command, stores the contents of FIL in DAT with the extension replaced then tests for the existence of the DAT file and outputs results.
  • Yes, this worked perfectly. Thanks so much.
  • mfillpot
    mfillpot Posts: 2,177
    I am glad that it worked, are there any portions of the command that you do not understand?

Categories

Upcoming Training