Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a small problem @ work. I am trying to write a script that will seach a directory for a file with the contents 'IS314.1'. Then move that file 2 a new directory. This is what I have but I am not sure I am using the right approach.
(I am using SH because that is all I am familiar with.)ls -l | sort +4 | sort +5 > tempfile.txt
cat tempfile.txt | while read file
do
filename=${file}
echo $filename
if $filename = uv*
then what?I am not sure I am takign the right approach.
Basically I need to find string IS314.1 in a file uv* and cp file to another directory as IS312.1Please help!

I know I am doing something wrong but so far I have modded what I have above to:
1. wrote a script to do:
ls -t uv* > listspl.txt
2. wrote a script to run after the main program runs (the one that generates IS314.1 - this cuts down on the files we need to look at)
ls -t uv* > listspl2.txt
diff -n listspl.txt listspl2.txt > listspl3.txt
cat listspl3.txt | grep uv > listsplfin.txt3. This part is breaking, I know something is feshmekeled but I can't figure out what I am doing wrong.
cat /u1/uvspool/listsplfin.txt | while read file
do
filename=${file}
cat /u1/uvspool/$filename | while read test
do
testme=${test}
if $testme = "IS314.1"
then
cp /u1/uvspool/$filename /u2/dbms/MHCDATA/DWNLD/IS314.1
fi
done
done

oh yea and the "IS314.1" string will be found within the first 10 lines of the file so it is not necessary to continue beyond that.

Ok I think I have step3 working now but if someone has a better (i.e. more ellegant solution) please let me know -
This is what I have:cat /u1/uvspool/listsplfin.txt | while read file
do
filename=${file}
if(grep IS314.1 /u1/uvspool/$filename)
then
cp /u1/uvspool/$filename /u2/dbms/MHCDATA/DWNLD/IS314.1
break
fi
done
cd /u1/dbms/LIVE.MHC
exit 0

#!/bin/sh
for file in `find . -exec grep -l IS314.1 {} \; ` ; do
cat $file >> /u2/dbms/MHCDATA/DWNLD/IS312.1
# you may have more than one occurance.
# should this overwrite or append?
done

actually I would like to keep the most reacent version. (mtime comes ot mind - but where do you put the command?) - There will only be one version on the day the script is run.
Also why do yo need the {}\;`;
This is all new to me so please explain. Also why grep -l?
I can't believe that you took all those lines and got it done in 2! I knew there was a better way but I am not that good at putting the commands togethers yet.
Thanks,
Mano

grep -l will print a file name where a match occurs and not the contents of the file. You can use this to generate a list of files that contain a substring you are looking for.
A find command that does more than the default print needs to be terminated bye "\;".
The "{}" represent the file matched by find.
To find and keep the most recent file, the find command would need to be piped through sort and head -1. I'll work on the details an post more of an answer. If there is only one version when the file is run you can do a cp instead of cat.
cp $file /u2/dbms/MHCDATA/DWNLD/IS312.1

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |