Shell script to search for text in a file and copy file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Shell script to search for text in a file and copy file

UNIX for Dummies Questions & Answers


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-03-2007
imeadows imeadows is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 3 August 2007, 11:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Power 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
Sponsored Links
    #2  
Old 08-03-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
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
Sponsored Links
    #3  
Old 08-03-2007
imeadows imeadows is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 3 August 2007, 11:47 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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-21-2008 at 12:50 AM.. Reason: added code tags
    #4  
Old 08-03-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts

Code:
egrep -l 'abcde1234' * | while read mFile
do
  cp ${mFile} /opt/efx/ian/copied
done

Sponsored Links
    #5  
Old 08-03-2007
quintet quintet is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 13 July 2011, 1:19 AM EDT
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
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-21-2008 at 12:49 AM.. Reason: added code tags
Sponsored Links
    #6  
Old 03-13-2008
kja328 kja328 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 13 March 2008, 3:20 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #7  
Old 04-19-2008
psychobeauty psychobeauty is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 22 April 2008, 6:58 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
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 06:30 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to build Shell Script to search content of a text file into a folder consist several files mukesh.baranwal Shell Programming and Scripting 3 09-16-2011 07:10 AM
shell script to take input from a text file and perform check on each servers and copy files joseph.dmello Shell Programming and Scripting 0 02-04-2011 07:34 AM
Perl or Awk script to copy a part of text file. asandy1234 Shell Programming and Scripting 26 11-06-2009 11:17 AM
To search a file for a specific word in a file using shell script girish.raos Shell Programming and Scripting 8 09-01-2009 06:51 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM



All times are GMT -4. The time now is 04:57 PM.