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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting files names and moving.

Status
Not open for further replies.

smithia6

Technical User
Aug 28, 2002
35
US
Hi I am just about to write a script but I need a little help to get me started.

I have a directory with txt files and the files names have different names at the start but they all have one thing in common, that they have a creation date in the file name E.G

file1_20031115.dat
anotherfile_20031115.dat
differentfile_20031116.dat

And so on .....

I want to get a script that I can run for example shellscript.ksh that I can add these dates on the end so it will pick up the files with these dates on it and copy the data out of them and place it all in one file into another directory.

E.G

shellscript.ksh 20031115 20031116 20031117

There will never be more than 5 different dates at the end but most times they will be between 3 - 4 dates.

Is this possable??

Thanks in advance.

Ian
 
Perhaps something like...

for i
do
cat *$i*
done >/some/file
 
I don’t suppose you could elaborate on that a little could you!

How do I pass the variables to the script if I add then at the start of the script how do I call back upon them within the script, and if I don't know how many I am going to pass at the start of the script can I just keep adding them on at the start.

Regards

Ian
 
try this:

Code:
for D in $*
do
  mv somefolder/*_$D.dat otherfolder
done

call it like that:

% move.ksh 20031115 20031116 20031117
 
The "for" contruct is a loop, which sets $i to each parameter in turn and executes cat.

for i #----1st time set i=20031115, 2nd time i=20031116, etc.
do
cat *$i* #----cat all filenames containing $i
done >/some/file
 
Where aam I going wrong here?????

#!/bin/ksh
for D in `$*`
do
mv &BAKUP/*_$D.dat ~/iandave/file1
done
cd ~/iandave
if test -f file1
then
cat file1 | cut -c3-12 | sort > ./file2
else
echo "No Files Exist"
exit
fi
for i in `cat mainfile`
do
grep $i ~/iandave/file2 > ./UniqFile
done
UniqFile=$(more ./UniqFile)
echo "$UniqFile"

I have tried it also with *_{$D}.dat still not working, if I do get this to work will it not just keep over writing the same file with a new entry??

Regards

Ian
 
for D in `$*`
change to

for D in "$*"


tikual
 
What are you trying to do? Extract the dates that are embedded in filenames in a directory ?

ls | awk 'match($0,"2003[0-9]{4}") {a[substr($0, RSTART, RLENGTH)]=1}
END {for(i in a) print i}' | sort
 
Perhaps something like this ?
Code:
#!/bin/ksh
for D in $*; do
  cat &BAKUP/*_$D.dat
done > ~/iandave/file1
cd ~/iandave
if [ -s file1 ]; then
  cut -c3-12 file1 | sort > file2
else
  echo "No File Exist for $*"; exit
fi
for i in `cat mainfile`; do
  grep $i file2
done > UniqFile
UniqFile=$(<UniqFile)
echo &quot;$UniqFile&quot;

Hope This Help
PH.
 
smithia6:

a) NO backslashes in $*
Backslases mean: run the command in them and replace the whole `command` with the command's output

b) &quot;mv&quot;ing to file1 overwrites file1 with the contents of the last file moved. By mistake I thought you wanted to move the files in another directory.

Code:
#!/bin/ksh
for D in $*
do
# here you can :
# > ~/iandave/file1
# if you want to empty previous contents of file1
  cat &BAKUP/*_$D.dat >> ~/iandave/file1
done
:
:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top