highly specific search and replace for a large number of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting highly specific search and replace for a large number of files
# 1  
Old 07-20-2010
highly specific search and replace for a large number of files

hey guys,

I have a directory with about 600 files. I need to find a specific word inside a command and replace only that instance of the word in many files. For example, lets say I have a command called 'foo' in many files. One of the input arguments of the 'foo' call is 'bar'. The word 'bar' many occur in many instances in a file but I want to replace only the 'bar' string inside the foo command call with bar1. I tried the following:
Code:
find . -type f -name *.c  | grep -l foo | xargs perl -ie "s/bar/bar1/g"

This finds all files with the foo command call but replaces all instances of bar with bar1 in those files. I am not sure how to make more specific and make it replace only the foo(bar) instance.

Thanks in advance !

Last edited by Franklin52; 07-20-2010 at 04:45 PM.. Reason: Please use code tags
# 2  
Old 07-20-2010
You've almost got it.
Code:
find . -type f -name *.c | grep -l foo | xargs perl -ie "s/foo\(bar\)/foo\(bar1\)"

or something like that. Figure out what uniquely identifies your foo command call and add that into the first part of the search and replace.
# 3  
Old 07-20-2010
"grep -l foo " is wrong, it greps on filename, not the file content.

Get the file list with foo in content:

Code:
find . -type f -name "*.c" -exec grep -l foo {} \;

# 4  
Old 07-20-2010
rdcwayx is absolutely correct. Good catch!
# 5  
Old 07-20-2010
Hi,

Try this,
Code:
perl -e 'while(<>){ s/(?<=foo)bar/bar1/; print $_; }' infile

inputfile
Code:
test foobar
test1 bar
testbar
test3bar
test5 foobar

output

Code:
test foobar1
test1 bar
testbar
test3bar
test5 foobar1

# 6  
Old 07-20-2010
Yea .. good catch .. I forgot to type 'xargs' before grep ..
Code:
find . -type f -name *.c  | xargs grep -l foo

This did the right thing and found all the relevant files.

I probably did not mention that the 'bar' can appear anywhere in the foo command call and there is no specific pattern. Will I be able use wildcards in the pattern matching string for 's/' and say inside the foo command change all occurrences of bar? I tried using foo\(.*)bar pattern but i keep getting the signal 13 error from grep or unmatched ) error. I am surely missing something obvious here. pointers?

---------- Post updated at 03:26 PM ---------- Previous update was at 02:57 PM ----------

I went a step further now ... I tried the following:
Code:
perl -ie 's/foo.*?bar/foo.*?bar1/'

It matched the proper pattern but instead of replacing the existing contents and only replacing bar with bar1, it replaced the whole thing with 'foo.*?bar1' which is not what I wanted.
Code:
foo(blah,blah,blah,bar,blah,blah)

should be changed to
Code:
foo(blah,blah,blah,bar1,blah,blah)

The number of blah's varies widely. How do I make it replace the pattern properly?

Last edited by Franklin52; 07-20-2010 at 04:47 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

2. Shell Programming and Scripting

Large search replace using sed results in memory problem.

I have one big file of size 9GB (big_file.txt). This big file has sentences and paragraphs like any usual English document. I have another file consisting of replacement strings for sed to use. The file name is replace.sed and each entry in one line looks like this: s/\<shout\>/shout/g s/\<b is... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. Shell Programming and Scripting

Search and replace ---A huge number of files

Hello Friends, I have the below scenario in my current project. Suggest me which tool ( perl,python etc) is best to this scenario. Or should I go for Programming language ( C/Java ).. (1) I will be having a very big file ( information about 200million subscribers will be stored in it ). This... (5 Replies)
Discussion started by: panyam
5 Replies

4. Shell Programming and Scripting

Sftp large number of files

Want to sftp large number of files ... approx 150 files will come to server every minute. (AIX box) Also need make sure file has been sftped successfully... Please let me know : 1. What is the best / faster way to transfer files? 2. should I use batch option -b so that connectivity will be... (3 Replies)
Discussion started by: vegasluxor
3 Replies

5. Shell Programming and Scripting

Help search and replace hex values only in specific files

perl -pi -e 's/\x00/\x0d\x0a/g' `grep -l $'GS' filelist` This isn't working :confused:, it's not pulling the files that contain the regex. Please help me rewrite this :wall:. Ideally for this to work on 9K of 20K files in the directory, I've tried this but I don't know enough about awk... (7 Replies)
Discussion started by: verge
7 Replies

6. UNIX for Dummies Questions & Answers

Delete large number of files

Hi. I need to delete a large number of files listed in a txt file. There are over 90000 files in the list. Some of the directory names and some of the file names do have spaces in them. In the file, each line is a full path to a file: /path/to/the files/file1 /path/to/some other/files/file 2... (4 Replies)
Discussion started by: inakajin
4 Replies

7. UNIX for Dummies Questions & Answers

How to replace specific number in a file

Hi, i'm trying to find a way to replace some numbers in a file, but with no luck sofar. Let's say i have file with three columns, i would like to find specific line, and replace third column with different number. search_string="oa11 /home/testfile1.log" new_number=600 test file... (4 Replies)
Discussion started by: dusoo
4 Replies

8. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. Shell Programming and Scripting

Creating large number of files of specific size

Hi I am new to shell scripting.I want to create a batch file which creates a desired number of files with a specific size say 1MB each to consume space.How can i go about it using for loop /any other loop condition using shell script? Thanks (3 Replies)
Discussion started by: swatideswal
3 Replies
Login or Register to Ask a Question