find pattern in FILES and replace it ??

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find pattern in FILES and replace it ??
# 1  
Old 02-28-2001


How can I looking for a pattern found in more than one file and replace it with anther pattern

this what I was used:

find . -name "account.adrs" -depth -follow -exec grep -l "Email = ;" {} \;

this print the files name -which is account.adrs- and its path -which is deferent for each file- which contains the pattern "Email = ;"

I need to replace this pattern by:
"Email = \"\" ;"

please help
# 2  
Old 02-28-2001
There are a few ways to do this. One example:


FILES=`find . -name account.adrs -depth -follow -exec grep -l "Email = ;" {} \;`

for file in $FILES; do
perl -p i.bak -e 's/Email = ;/Email = \"\" ;/' $file

# 3  
Old 03-01-2001
perl -p i.bak -e 's/Email = ;/Email = \"\" ;/' $file
This generates an error for me "Can't open Perl script i.bak".

Does it mean we need a perl script named "i.bak" to do this task. I am not good at perl from command line.

here is another script. little modification from PxT's Script


FILES=`find . -name "account.adrs" -depth -follow -exec grep -l "Email = ;" {} \;`

for file in $FILES; do
sed -e 's/Email = ;/Email = \"\" ;/' $file > temp
mv -f temp $file

# 4  
Old 03-01-2001
The i.bak is a directive to create backups of the original file with an extension of '.bak'. This helps insure that if you make mistake in your PERL filter, you have a backup of the original (always a good idea !). I did not check the exact syntax in the example code provided.
# 5  
Old 03-03-2001
Thank you for info Neo.

error I made was, I put space between -p and i.bak.

it should be -pi.bak(without space) or -p -i.bak

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

2. Shell Programming and Scripting

Search pattern then find and replace

If condition satisfy, want to find pattern and modify two Fields in Modify.txt Input.txt SOURCE1 SOURCE2 SOURCE3 SOURCE4 SOURCE5 SOURCE6 Modify.txt SOURCE1|SLA|2016/12/11 11:12:11 PM|HMM|11-11-16| SOURCE2|SLA|2016/13/11 11:12:11 PM|HMM|10-11-16| SOURCE3|SLA|2016/14/11 11:12:11... (7 Replies)
Discussion started by: Joselouis
7 Replies

3. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route" push "route" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

4. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e keyword... (1 Reply)
Discussion started by: yart
1 Replies

5. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

6. Shell Programming and Scripting

find a pattern and replace

i have a file which contains lines like this. intsrcrpttrn1099mctrl:export GRAPHPARM_AR="-input_code M302023" intsrcrpttrn1099mload:export GRAPHPARM_AR="-input_code M192023" intsrcrpttrn1099mload:export GRAPHPARM_AR="-input_code P192023" the value after -input_code starts with some alphabet... (4 Replies)
Discussion started by: dr46014
4 Replies

7. Shell Programming and Scripting

Find and replace pattern in VI editor

All, I have a text file which has the following data X_SQL_13,X_SQL_14,X_SQL_15,X_SQL_16,X_SQL_17,X_SQL_18,X_SQL_19,X_SQL_20,X_SQL_21,X_SQL_22,X_SQL_23,X_SQL_24,X_SQL_25,X_SQL_26,X_SQL_27,... (4 Replies)
Discussion started by: thana
4 Replies

8. Shell Programming and Scripting

Find a pattern and replace using sed.

Hi I need to help on finding the below pattern using sed <b><a href="/home/"> and replace as below in the same line on the index file. <b><a href="/abcde.html"> thx in advance. Mari (5 Replies)
Discussion started by: maridhasan
5 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies

10. Shell Programming and Scripting

find and replace a pattern in a file

Hi I am having 2 files file1.c and file2.c Now i want to find all the occurances of pattern "abc" in file1.c, file2.c and replace with pattern "def" using shell script without using sed and with using sed. Thanks in advance... raju (1 Reply)
Discussion started by: krishnamaraju
1 Replies
Login or Register to Ask a Question