Linux.com

Home Linux Community Forums New to Linux Getting Started with Linux using find command for multiple files and then manipulating the results

using find command for multiple files and then manipulating the results

Link to this post 29 Oct 13

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

Link to this post 30 Oct 13

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.

Link to this post 31 Oct 13

Yes, this worked perfectly. Thanks so much.

Link to this post 01 Nov 13

I am glad that it worked, are there any portions of the command that you do not understand?

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board