grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:.

From what little info you have given, that is the basic function of grep.
Show us what you did so far for us to understand your requirement.
May be you should look into "fgrep" instead of "grep".

strings MR* | grep MoCoseries (actually that works o.k.)

getting that into the filename for files that contain the string is giving me problems. I tried:

STR=strings MR* | grep MoCoseries

mv MR* MR*$STR

.....and alot of other things!

Try this:

STR=$(strings MR* | grep MoCoseries)

Beware: you have to code for "What happens if the string is not found"

1 Like

Great - That got it - Thanks!

for i in $(ls mr*); do
  STR=$(strings $i | grep MoCoSeries)
  mv $i $i$STR 
done