LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep files based on strings (https://www.linuxquestions.org/questions/linux-newbie-8/grep-files-based-on-strings-4175422457/)

verse123 08-16-2012 02:16 PM

grep files based on strings
 
how can I search the contents of a directory for a string within a certain file that matches the name of a text file within the directory? And if the file within the directory is matched by the string, then move that file into a new directory.

So for example:

I have a text file like this:

Code:

$ cat f2
Quote:

example_Name
test
helloWorld
blahblah23
and in a directory I have a long list of files (~14,000) that looks like this:

Code:

$ ls ./est-07
Quote:

example_Name.txt
test.txt
helloWorld.txt
example1.txt
example.txt
ect.txt
blahblah23.txt
..
..
...

Therefore, the output should

Code:

$ ls ./newDir
Quote:

example_Name.txt
test.txt
helloWorld.txt
blahblah23.txt

so far, I have this:

Code:

while read A B; do grep -r $A *.txt | mv ../newDir; done < ../f2 > ../f2.out
but I am not sure this will work... I am also getting this error

Quote:

-bash: /bin/grep: Argument list too long
Any idea how to fix this or come up with a code that works?

verse123 08-16-2012 02:32 PM

this for loop prints the file name in the different directory but not the content of each file.

Code:

for i in *.txt; do cat $i | grep -f ../f2 > ../newDir/$i.out ; done
=/ still stumped

wpeckham 08-16-2012 02:35 PM

One answer
 
Well, grep is not your best tool here. You could use it, but something like this little shell script might be better:
Code:

#!/bin/bash
SRC=<the directory with the files and the file est-07 here>
DEST=<the target directory here>

cd $SRC
while read foo bar ; do
  if [ -e ${foo}.txt ] ; then
      echo "found file ${foo}.txt"
      mv  ${foo}.txt  ${DEST}/${foo}.txt
  else
      echo "no match on ${foo} "
  fi
done < est-07

cd $SRC
ls


verse123 08-16-2012 03:30 PM

This works great. I could not assign the SRC and DEST variables for whatever reason but the script worked fine when I removed these variables and just used the paths.

I kept getting this error with the variables
Quote:

/newDir/: No such file or directory

montel 08-16-2012 05:29 PM

I believe this would work for you too?

Code:

#!/bin/bash
while read line; do
    find ./est-07/ -name $line.txt -exec cp {} ./newDir/ \;
done < f2


Celyr 08-16-2012 05:36 PM

Quote:

Originally Posted by verse123 (Post 4755907)
This works great. I could not assign the SRC and DEST variables for whatever reason but the script worked fine when I removed these variables and just used the paths.

I kept getting this error with the variables

Because you started with a / and it was looking for that directory at root.
you can use DEST="newDir"


All times are GMT -5. The time now is 04:20 PM.