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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • Posts: 2,177
    how about something like:
    1. #!/bin/bash
    2. CWD="$(pwd)"
    3. for FIL in $(find . -name *.trg)
    4. do
    5. DAT=$(echo "$FIL" | sed 's/\.trg/\.dat')
    6. if [ -f $CWD/$DAT ]; then
    7. echo "$FIL and & $DAT exist"
    8. else
    9. echo "$DAT does not exist"
    10. fi
    11. 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.
  • Posts: 2,177
    I am glad that it worked, are there any portions of the command that you do not understand?

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training