Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read All files and append to another files 1

Status
Not open for further replies.

jmanj

Programmer
May 20, 2003
298
US
Need your help on this using ksh (or maybe awk).

I want to read all files is a directory that has an extension of .dmx and then append the data to an empty file. This .dmx file has a timestamp on it's name and I also want to delete it once the content is copied to the other file.

Sorry but I lost my PC hence I lost lots of sample scripts.
It's been so long since my I wrote my last script.

Thanks for any help.
 
If I understand your requirements correctly, this should be enough to do it:

Code:
cat *.dmx > newfile
rm *.dmx

Annihilannic.
 
Thanks Annihilannic. I forgot about cat.
Should it be Cat *.dmx >> newfile ?

Also rm *.dms will remove all files with that extension.
My purpose is only to remove one at a time once it is copied
to newfile.

Thanks a lot for shaking my memory.
 
Please see below sample script if I'm on the right track:

# PROCESS ALL *.dmx FILES WITHIN WRKDIR DIRECTORY

cd WRKDIR

find . -type f -name "*.dmx" -print | cut -c 3-500 | grep -v '/' > "${FOUND}"

### or should it be

find . -type f -name "*.dmx" -print | grep -v '/' > "${FOUND}"

## Process the file if any

cat "${FOUND}" | while read wrkFILES
do
cat $wrkFILES > NEWFILE
rm $wrkFILES
done
 
Annihilannic's is the quickest and easiest. Simple equals fewer chances for bugs and errors (a star for Anni).

Why do you have the "[tt]grep -v '/'[/tt]" after the find? Is this to exclude any that are in subdirectories? If so, just don't use the "[tt]find[/tt]" command. It looks like you're trying to emulate an "[tt]ls[/tt]" command using "[tt]find[/tt]" and "[tt]grep[/tt]". Never make something more complicated than it needs to be.

If you want to do your script method, this would be more correct.
Code:
#!/bin/ksh

WRKDIR=/some/directory
NEWFILE=/path/to/alldmx.txt

ls -1 ${WRKDIR}/*.dmx | while read DMXFILE
do
    cat ${DMXFILE} >> ${NEWFILE}
    rm  ${DMXFILE}
done
And there are all kinds of variations possible on that. Just remember to use ">>" instead of ">" when you append the file. Your example used ">", so it would only contain the contents of the very last file and nothing else.



 
This is way much better. Thanks for the help.

I will modify a little bit. In removing the file I will
have to qualify it with the directory name:

rm $WRKDIR/${DMXFILE}
 
That form of the "[tt]ls[/tt]" command will include the directory, so you're good. This will show you...
Code:
#!/bin/ksh

WRKDIR=/some/directory
NEWFILE=/path/to/alldmx.txt

ls -1 ${WRKDIR}/*.dmx | while read DMXFILE
do
    [highlight]print "Appending file: ${DMXFILE}"[/highlight]

    cat ${DMXFILE} >> ${NEWFILE}
    rm  ${DMXFILE}
done


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top