LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2012, 02:16 PM   #1
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Rep: Reputation: Disabled
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?
 
Old 08-16-2012, 02:32 PM   #2
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-16-2012, 02:35 PM   #3
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,648

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
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
 
1 members found this post helpful.
Old 08-16-2012, 03:30 PM   #4
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-16-2012, 05:29 PM   #5
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
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

Last edited by montel; 08-16-2012 at 05:31 PM. Reason: $line.txt, not $line*
 
1 members found this post helpful.
Old 08-16-2012, 05:36 PM   #6
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 319

Rep: Reputation: 80
Quote:
Originally Posted by verse123 View Post
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursive search for strings in files with a certain date: find -name or grep -R? wolverene13 Linux - Newbie 6 10-01-2011 06:05 PM
Grep -- finding files that contain two separate strings Fliggerty Linux - Newbie 4 12-10-2010 09:40 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 03:40 PM
Script to grep log files based on system date cyclegar Programming 2 05-05-2009 07:08 PM
grep two strings Hondro Linux - General 3 09-08-2008 10:55 PM


All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration