Forum OpenACS Q&A: Hack wanted: dir name and text in file

Hi OpenACS team,

I need a HACK,
meaning, the simplest easiest fastest way to do the following:

We have hundreds of directories with only one file in each directory.

Needed: a list with two items:
The directory name and the id number within the single file.
Example:

dirname 123
bobdir  456
billdir 3444

The file is small with only two lines in it and the id number
Example of text in file:
returnredirect "/comunity/myface.tcl?is=11730"
All I need is the id# from this text in the file.

Yup, we should have been using a modified version of homepage-defs.tcl long ago to do this automatically but at the time it was expedient to to it manually!

THANK YOU.
-Bob OConnor

Collapse
Posted by Jerry Asher on
Hi Bob,

If I understand what you are trying to do, it is something that egrep and sed should be able to cons up for you. Something like this example here:

[root@web log]# cd /tmp
[root@web /tmp]# mkdir rocnet
[root@web /tmp]# cd rocnet
[root@web rocnet]# mkdir a
[root@web rocnet]# mkdir b
[root@web rocnet]# mkdir c
[root@web rocnet]# echo "ns_returnredirect "/foo/bar.tcl?id=11730"" > a/afoo
[root@web rocnet]# echo "ns_returnredirect "/foo/bar.tcl?id=11731"" > b/bfoo
[root@web rocnet]# echo "ns_returnredirect "/foo/bar.tcl?id=11732"" > c/cfoo
[root@web rocnet]# egrep -r "[0-9]*" *
a/afoo:ns_returnredirect "/foo/bar.tcl?id=11730"
b/bfoo:ns_returnredirect "/foo/bar.tcl?id=11731"
c/cfoo:ns_returnredirect "/foo/bar.tcl?id=11732"
[root@web rocnet]# egrep -r "[0-9]*" * | sed -e "s//.*=/ /" -e "s/".*$//"
a 11730
b 11731
c 11732
[root@web rocnet]# 
Does that help?
Collapse
Posted by Jerry Asher on
I've been using unix since '84 and that was my first sed script.  That's cause I've been using emacs since '80.
Collapse
Posted by Marc Spitzer on
This is not tested but should work
#!/bin/sh

for i in *
do
    X=`cat $i/*`
    echo "$i $X"
done
Collapse
Posted by Marc Spitzer on
I may have been a bit abrupt on that last post.  egrep on solaris is not recursive, if I remember correctly.
Collapse
Posted by Bob OConnor on

Thanks Jerry and Mark.
Both techniques work!

Jerry's second egrep didn't work due to my specifics:
egrep -r "[0-9]*" * | sed -e "s//.*=/ /" -e "s/".*$//"

I haven't groked what sed is doing...

-Bob