1823422 Members
2434 Online
109655 Solutions
New Discussion юеВ

Scripting help

 
SOLVED
Go to solution
Duke Nguyen
Occasional Advisor

Scripting help

Need some help/ideas with writing a script that will do the following:
1. Scripts will need to scan a directory and find files begining w/ abc*
2. Then append all abc* files that were found to a new file named xyz.dat
3. After running another script to process xyz.dat file, will then need to delete only the abc* files that were found and the xyz.dat file.

Any suggestions would be appreciated.

Thanks!
11 REPLIES 11
Rodney Hills
Honored Contributor

Re: Scripting help

How about

cat abc* >xyz.txt
sh yourscript
rm abc* xyz.txt

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Scripting help

Duke,

cd to appropriate directory then

'find . -type f -name "abc*" >> xyz.dat'

As to question (3), what kind of processing would be done on xyz.dat?

To delete the files that are listed in xyz.dat, you can do this:

cat xyz.dat | xargs -i rm {}


live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: Scripting help

Hi,

How about:

===============================
#!/bin/ksh

dir=$1
cd $dir
find . -name "abc*" > xyz.dat
...
... process xyz.dat
...
cd $dir
cat xyz.dat | xargs rm
rm xyz.dat
==============================

then run with:

yourscript.sh directory-to-process

Rgds, Robin.
Santosh Nair_1
Honored Contributor

Re: Scripting help

Are you trying to append the contents of the abc* files to xyz.dat? If so, how about:

for FILENAME in (find . -name abc\*)
do
cat $FILENAME >>xyz.dat
FILES="$FILES $FILENAME"
done
... process xyz.dat

rm $FILES

-Santosh
Life is what's happening while you're busy making other plans
Duke Nguyen
Occasional Advisor

Re: Scripting help

Thanks for the quick responses.
I should have been more clear, but yes...I need to append the contents of the abc* files to xyz.dat. File xyz.dat is data that will be processed by a sql script.
harry d brown jr
Honored Contributor

Re: Scripting help

AHHHH, the "contents" of the abc* files:

'find . -type f -name "abc*" | xargs -i cat {} >> xyz.dat'

live free or die
harry
Live Free or Die
Duke Nguyen
Occasional Advisor

Re: Scripting help

Harry - How would I go about removing the files that were found by the find command? I can't do a rm abc*. I only want to remove files that were found w/ the find command.

Santosh - I tried your script but get the following error:

/tmp # for FILENAME in (find . -name abc\*)
sh: Syntax error: `(' is not expected.
Robin Wakefield
Honored Contributor

Re: Scripting help

Hi,

In that case, how about:

===============================
#!/bin/ksh

dir=$1
cd $dir
find . -name "abc*" > abc.dat
cat abc.dat | xargs cat >> xyz.dat
...
... process xyz.dat
...
cd $dir
cat abc.dat | xargs rm
rm xyz.dat
==============================

then run with:

yourscript.sh directory-to-process

Rgds, Robin.
Eugen Cocalea
Respected Contributor
Solution

Re: Scripting help

Hi,

find . -name abc\* -exec echo {} >>list_abc \;
find . -name abc\* -exec cat {} >>xyz.dat \;

execute the script

rm `cat list_abc` list_abc xyz.dat

Can be improved by making just one find, but since I am not a scripting wizard, like Clay for example, I couldn't make it in 5 minutes.

If it would be possible to make something like:

find . -name abc\* -exec echo {} >>list_abc && cat {} >>xyz.dat \;

it would be shorter. But it works with two 'find's anyway.

E.
To Live Is To Learn
Eugen Cocalea
Respected Contributor

Re: Scripting help

Hi,

Got it!

find . -name abc\* -fprint list_abc -exec cat {} >>xyz.dat \;

execute script

rm `cat list_abc` list_abc xyz.dat

E.
To Live Is To Learn
Duke Nguyen
Occasional Advisor

Re: Scripting help

Thanks for all your help!