LinuxQuestions.org
Help answer threads with 0 replies.
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-28-2008, 03:26 AM   #1
abenmao
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Rep: Reputation: 0
Question copy files containing specific words in a specified line


Anyone knows how to use bash script to copy / move files containing particular words in a specified line to other folders?

for example:

lots of text files *.txt in a folder /tmp
I'd like to move files containing "my book" in the line 9 to another folder /home/abenmao/my book/, and move files containing a word "my dog" in the line 9 to another folder /home/abenmao/my dog/ ...

Thank you for looking into my question
 
Old 08-28-2008, 03:40 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 38
grep with the -l switch will list only the file names. Test it first:
Code:
grep -l "my book" *
Now use cp to copy the hits to somewhere else.
Code:
cp `grep -l "my book" *` /home/abenmao/mybook/
Repeat with 'my dog' instead of 'my book'.
 
Old 08-28-2008, 03:47 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
A requirement is "pattern in line 9". I would keep it simple and use a loop like this:
Code:
#!/bin/bash
while read file
do
  if sed -n 9p "$file" | grep -q "my book"
  then
     mv "$file" "/home/abenmao/my book/"
  elif sed -n 9p "$file" | grep -q "my dog"
  then
     mv "$file" "/home/abenmao/my dog/"
  fi
done < <(find /tmp -type f -name "test*")
As a general tip, I would not use blank spaces in the directory names. This makes things a bit more complicate in shell scripting. Use _ (underscore) instead.
 
Old 08-28-2008, 07:30 AM   #4
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 38
ehh.. missed the line-9 requirement. Your script is pretty neat.
 
Old 08-28-2008, 08:56 AM   #5
abenmao
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Original Poster
Rep: Reputation: 0
thanks,colucix, your code worked like a charm. thanks a million! linux is really really powerful. a very long way for me to go!
 
Old 08-28-2008, 09:04 AM   #6
abenmao
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Original Poster
Rep: Reputation: 0
never expect my question will be answered in such a short time. thanks

Quote:
Originally Posted by colucix View Post
A requirement is "pattern in line 9". I would keep it simple and use a loop like this:
Code:
#!/bin/bash
while read file
do
  if sed -n 9p "$file" | grep -q "my book"
  then
     mv "$file" "/home/abenmao/my book/"
  elif sed -n 9p "$file" | grep -q "my dog"
  then
     mv "$file" "/home/abenmao/my dog/"
  fi
done < <(find /tmp -type f -name "test*")
As a general tip, I would not use blank spaces in the directory names. This makes things a bit more complicate in shell scripting. Use _ (underscore) instead.
 
  


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
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-07-2013 11:34 PM
How to copy a specific no of files from a group of files biswa_nayak Programming 2 05-11-2008 12:21 PM
BASH script to copy specific files ScottReed Linux - Software 5 07-20-2006 11:08 AM
copy specific files with directory stucture rincewind Linux - Software 1 06-22-2006 07:58 AM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM


All times are GMT -5. The time now is 10:44 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration