Reputation: 4663
$ a=$(find . -iname 'app.conf' 2>/dev/null)
The above gives me a list of files shown below:
$ echo "$a"
./etc/apps/dashboard_examples/default/app.conf
./etc/apps/framework/server/apps/homefx/splunkd/default/app.conf
.
.
.
How could I do a cat, and then grep, on each one of these files?
Could I do cat on the first element in the array e.g. cat a[1]
? Or how do I put a in a format like an array?
Upvotes: 0
Views: 234
Reputation: 290185
You can store the result of find
into an array using the myarray=( $(command) )
expression:
a=( $(find . -iname 'app.conf' 2>/dev/null) )
# ^ ^
Then, print the first element with:
echo "${a[0]}"
Or if you want to cat
it, say:
cat "${a[0]}"
If you want to execute a command on every result, you can use -exec
as indicated by Etan Reisner in the comments:
find . -iname 'app.conf' -exec cat {} + 2>/dev/null
# ^^^^^^^^^^^^^^
Upvotes: 1
Reputation: 413
How about using for?
> for a_file in $a; do cat $f | grep something; done
Upvotes: 0
Reputation: 312370
I would use xargs
to do most of those things.
find . -iname 'app.conf' 2>/dev/null |
xargs grep somepattern
You can operate safely on filenames with spaces by using -print0
to find
and -0
to xargs
.
See man xargs
for more information.
Upvotes: 0