I have text files in multiple subdirectories
some/path/one/file
some/path/two/file
some/path/three/file
some/path/foo/file
...
I need to be able to grep file for a match, but retain the individual line returns so that I can operate on each match individually with a loop later.
I have:
SITE=$(grep -w $1 some/path/*/file)
however, this just produces a really long string with everything just smooshed together in one line.
Ideally, This variable would contain out the value of * from the pathname on each line:
for example:
some/path/one/file
some/path/three/file
contain a line that has bar
in it.
SITE=$(grep -w bar some/path/*/file)
echo $SITE
should look like this:
some/path/one/file:this is a line of text that has bar in it
some/path/three/file:this is a line of text that has bar in it
but instead, I get this:
some/path/one/file:this is a line of text that has bar in it some/path/three/file:this is a line of text that has bar in it
Is there a way to make this variable multi-line so that later I can do stuff like:
for i in `echo $SITE`; do
WHATEVER=$(echo $i | cut -d ':' -f 1 | cut -d '/' -f 3)
echo $WHATEVER
done
and have the output be:
one
three
Or am I going about this entirely the wrong way?