how to find a pattern from an external file in a directory containing multiple file recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find a pattern from an external file in a directory containing multiple file recursively
# 1  
Old 10-20-2012
how to find a pattern from an external file in a directory containing multiple file recursively

Hi,

Need your help in this.

I have an input file that has multiple enrollment_number, somewhat like
Code:
1234567
8901234
9856321
6732187
7623465

Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a directory.
# 2  
Old 10-20-2012
Code:
# your input file is called infile in this example
find /directory -type f |
while read fname 
do
    grep -v -f infile  $fname > tmp.tmp
    mv tmp.tmp $fname
done

# 3  
Old 10-21-2012
Hi Jim,
It isn't working for me getting below error :
Code:
grep: /tmp/myproject/content_to_remove/config/Willy: No such file or directory
grep: Mukabya/SUBCAT_INFO.cfg: No such file or directory
mv: when moving multiple files, last argument must be a directory

---------- Post updated at 09:12 AM ---------- Previous update was at 08:19 AM ----------

This is what I've tried, but its not working either :
Code:
#!/bin/bash
while read line
do
    for i in $(find /tmp/myproject/content_to_remove/config/ -type f)
    do
        #grep "$line" "$i"
         sed 's/$line//g' $i
    done
done < "/tmp/myproject/content_to_remove/infile.txt"

# 4  
Old 10-21-2012
Try this:-

Code:
#!/bin/bash
while read line
do
    for i in $(find /tmp/myproject/content_to_remove/config/ -type f)
    do
         DIR=`echo $i | sed 's|\(.*\)/.*|\1|'`
         sed 's/'$line'//g' $i > ${DIR}/tmp; mv ${DIR}/tmp ${i}
    done
done < "/tmp/myproject/content_to_remove/infile.txt"


Last edited by Yoda; 10-21-2012 at 01:34 PM..
This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-21-2012
@mukulverma2408, Jim's suggestion should work for you (assuming that you want to delete whole lines containing the pattern). But, it seems that your file names are having spaces in between. So, try this small variation of Jim's script:
Code:
# your input file is called infile in this example
find /directory -type f |
while read fname 
do
    fgrep -vf infile "$fname" > tmp.tmp
    mv tmp.tmp "$fname"
done

@bipinajith
try to avoid using sed on single lines. What you wish to achieve could easily be done by the shell:
Code:
DIR=${i%/*}

And if line happens to contain a string with spaces, it will break the sed command (since you've not enclosed the variable expansion with double-quotes).

Last edited by elixir_sinari; 10-21-2012 at 01:57 PM..
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-21-2012
hi,

Code:
find $directory -type f -exec sed 's/'"$(sed ':r;N;$!;br;s/\n/\\|/g' enrollment.file)"'//g' \+

# 7  
Old 10-22-2012
Thanks elixir,

but seems like something is missing somewhere, now the script is running fine but is not performing the intended work, the enrollment number are not getting deleted from file/s (inside the directory named config) instead my "infile.txt" is getting empty every time i execute the script.

@bipinajith : the script is working fine and the enrollment number is getting replaced could you please explain the code also.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Find file type recursively and move

Hello, I supposed that it was working fine but now I see that it's not working as expected. I am running under ubuntu14.04, trusty. My plan was to search folderA and all subdirectories and move any txt file to destination folder, folderB : find /home/user/folderA/ -type f -iname "*.txt"... (0 Replies)
Discussion started by: baris35
0 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

Find Large Files Recursively From Specific Directory

Hi. I found many scripts in the web of achieving this. But I like to use this one find /EDWH-DMT03 -xdev -size +10000 -exec ls -la {} \;|sort -n -k 5 > LARGE.rst But the problem is, why it still list out files with 89 bytes as the output? Is there anything wrong with the command? My... (7 Replies)
Discussion started by: aimy
7 Replies

5. Shell Programming and Scripting

Loop multiple directory, find a file and send email

Hello ALL, need a BASH script who find file and send email with attachment. I have 50 folders without sub directories in each generated files of different sizes but with a similar name Rp01.txt Rp02.txt Rp03.txt ...etc. Each directors bound by mail group, I need a script that goes as... (1 Reply)
Discussion started by: penchev
1 Replies

6. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

7. Shell Programming and Scripting

Find a particular directory in multiple file systems and calculate total size

Hello : I need some help in writing a ksh script which will find a particular directory in all the file systems in a server and finally report the total size of the direcotry in all the file systems. Some thing like this.. find /u*/app/oracle -type d -name "product" -prune and then... (1 Reply)
Discussion started by: Sam1974
1 Replies

8. Shell Programming and Scripting

Need to find recursively all shell script in the /xyz directory

Pls. advise how to find or used grep recursively all shell script files. Some files doesnt have a .sh or .ksh extension name. find / -name "*" |xargs grep bin |grep sh ?? TIA (1 Reply)
Discussion started by: budz26
1 Replies

9. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

10. Shell Programming and Scripting

How to pass multiple file types search pattern as argument to find?

How can I pass $var_find variable as argment to find command? test.sh var_find=' \( -name "*.xml" -o -name "*.jsp" \) ' echo "${var_find}" find . -type f ${var_find} -print # Below statement works fine.. I want to replace this with the above.. #find . \( -name "*.xml" -o -name... (4 Replies)
Discussion started by: kchinnam
4 Replies
Login or Register to Ask a Question