The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to search content of file with timestamps in the directory psychobeauty Shell Programming and Scripting 10 04-21-2008 02:37 AM
Shell script for text extraction from a file vignesh53 Shell Programming and Scripting 3 02-05-2008 04:16 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 07:17 PM
Shell script to return all the ID's from file based on the distribution ID search kumbhatalok UNIX for Dummies Questions & Answers 1 10-06-2006 09:53 AM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 04:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-03-2007
Registered User
 

Join Date: Aug 2007
Posts: 2
Stumble this Post!
Red face Shell script to search for text in a file and copy file

Compete noob question....

I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory

help please
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-03-2007
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 767
Stumble this Post!
this will list all files in the given directory that contain the given string:
Code:
find /path/to/dir/ * | xargs grep abcde1234 | cut -d ":" -f1 | uniq
remaining is left for you to do
Reply With Quote
  #3 (permalink)  
Old 08-03-2007
Registered User
 

Join Date: Aug 2007
Posts: 2
Stumble this Post!
Thanks, have knocked this script up, what do you think
Code:
#!/bin/bash
files="$(find /opt/efx/ian/original | xargs grep abcdef123 | cut -d ":" -f1 | uniq)"
echo $files
for X in $files
do
cp $X /opt/efx/ian/copied
done

Last edited by Yogesh Sawant; 04-20-2008 at 09:50 PM. Reason: added code tags
Reply With Quote
  #4 (permalink)  
Old 08-03-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Stumble this Post!
Code:
egrep -l 'abcde1234' * | while read mFile
do
  cp ${mFile} /opt/efx/ian/copied
done
Reply With Quote
  #5 (permalink)  
Old 08-03-2007
Registered User
 

Join Date: Jun 2006
Posts: 74
Stumble this Post!
alternatively u can do like this

Code:
find /opt/efx/ian/original  -name '*' -exec grep -l "abcde1234" {} \; -exec cp {} /opt/efx/ian/copied \;

Last edited by Yogesh Sawant; 04-20-2008 at 09:49 PM. Reason: added code tags
Reply With Quote
  #6 (permalink)  
Old 03-13-2008
Registered User
 

Join Date: Mar 2008
Posts: 1
Stumble this Post!
question

what type of files will this work on ? I have a need for this script to find terms in documents such as word and excel spreadsheets.
Reply With Quote
  #7 (permalink)  
Old 04-19-2008
Registered User
 

Join Date: Apr 2008
Posts: 17
Stumble this Post!
if u want to input in each time what the script to find u???
what do u change??

Last edited by psychobeauty; 04-19-2008 at 03:30 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0