Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello!
I'm trying to write a shell script, but don't know where to start. I need to do the following:
- go into each directory, check for a file named "text"
- if the file exists in that directory, I need to run an awk program I wrote.
- this also needs to check any subdirectories.I'm not even sure where to start except perhaps something like:
find . -name 'text' | exec {} vnmr.awk
I don't know....
Thanks for any help in advance!

Hi:
Here's one way:
find . -type f -name "text" -print| while read obj
do
yourawkprogram $obj
done
# end scriptRegards,
Nails

> find . -name 'text' | exec {} vnmr.awk
"-exec" is part of "find", not a separate command, and "{}" stands for the current file name returned by find. The syntax for this would therefore be:
find . -name 'text' -exec vnmr.awk {} \;

One thing I should've mentioned. I need to get the one output file from the awk program in each of the directories where the text file was found. Nails, you're suggestion worked well, but it only created a single file in the dir where I ran the script from. Any more ideas? I appreciate the help!
Higgies

Higgies:
I'm still not certain what you want to do, but how about getting the basename of the file object and passing that to your awkscript to build an output file?
The output of find looks like this:
./dir1/file1
./dir2/file2Basename gives you:
./dir1
./dir2You don't mention which shell you're using, but this gets you the dirname under sh:
find . -type f -name "text" -print| while read obj
do
f=`dirname $obj`
echo $f
doneIf you use ksh, this uses ksh pattern matching operators to do the same thing:
find . -type f -name "text" -print| while read obj
do
f=${obj%/*}
echo $f
doneRegards,
Nails

That's exactly what I'm trying to do. I need to go into each subdirectory, see if there is a file named 'text'. If there is, then execute the awk program on it and redirect the output to a file 'text.out' in the same directory as the 'text' file.
The problem I'm having is if I use something like you gave me...
f=${obj%/*}
then f = ./dir1/filename
not ./dir1I tried string manipulation to remove the last 5 characters from that...but it didn't work. is there some other syntax which would get me that?
I'm using BASH to develop this, but it will likely be run in ksh. They are pretty compatible.
Thanks for your help again.

Nails! You da man!
Ok, I tried it again, this time I redirected output from the awk command to:
> ${obj%/*}/text.out
And it works exactly as I needed it to!
I appreciate the responses!
Higgies

I have a problem with shell programming as well.. are there any string manipulation functions? for example I read a line: "different ./temp".. "temp" is a file name and I want to get that name separately in order to do some separate processing on it, can I do that?
something similar to "substring" may be?I'd really appreciate the response....
regards,
heba

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

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