I'm trying to search the content of all the files in directory B which might contain a string that matches a list of file names from another directory (a).
For instance, if directory a
contains a list of files that their content either contains STR1
or STR2
, I can get a list the name of the files (minus the type) with:
cd /dir/a/ && grep 'STR1\|STR2' * | awk -F. '{print $1}' | uniq -u
which produces something like:
filename1
filename7
filename13
filename24
Now, I need to get the list outputted by this command (first column, eg filename1, filename7, etc.), and search the content of each file in /dir/b/*
to see if any of item in the list is available in the list above. If I try this:
grep $(grep 'STR1\|STR2' /dir/a/* | awk -F. '{print $1}' | uniq -u) /dir/b/*
I get multiple grep somestr_from_command: no such file or directory
, and only one of the items from the list is searched for availability in /dir/b/
files.
How can I transform the output of grep 'STR1\|STR2' * | awk -F. '{print $1}' | uniq -u
grep 'STR1\|STR2' * | awk -F. '{print $1}' | uniq -u
grep 'STR1\|STR2' * | awk -F. '{print $1}' | uniq -u
to something like grep 'filename1|filename7|filename13|filename24' …
so I can be fed to the outermost grep
?
You can use -f
option in grep
with a process substitution for another command:
grep -Ff <(awk '/str1|str2/{s=FILENAME; gsub(/^.+\/|\.[^.]+$/, "", s); print s; nextfile}' /dir/a*) /dir/b*
Also note that a single awk
command can replace grep | awk | uniq
grep | awk | uniq
grep | awk | uniq
pipeline.
You can do something along these lines:
for fn in *; do
if [ -f "$fn" ]; then
awk ' /STR1|STR2/{ split(FILENAME,f,"\\."); print f[1]; nextfile}' "$fn"
fi
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.