Error: Find the pattern in the file and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error: Find the pattern in the file and replace
# 1  
Old 03-04-2009
Error: Find the pattern in the file and replace

Hi all,

I have requirement where i need to find a pattern and replace it by new word. I used the below perl command

echo `perl -p -i -e "s/AbCdEf/PqRsTu/g;" FileName.txt`

But I am getting an error as below


Can't do inplace edit Not a Owner

and the file FileName.txt gets deleted from that folder.
Is there any other options (With out using a intermediate file).
# 2  
Old 03-04-2009
Hi Krish,
You can try

sed -i -e's/SEARCH_STRING/REPLCE_STRING/g;' <filename>

if at all you wanna work with sed .I mean other than perl
# 3  
Old 03-04-2009
sed s/"$OSTRING"/"$NSTRING"/g $FILENAME > NEWFILE
# 4  
Old 03-04-2009
Quote:
Originally Posted by gauravacl
Hi Krish,
You can try

sed -i -e's/SEARCH_STRING/REPLCE_STRING/g;' <filename>

if at all you wanna work with sed .I mean other than perl

Hi Gaurav,

I tried using sed , it gave the follow error

sed: illegal option -- i
Usage: sed [-n] Script [File ...]
sed [-n] [-e Script] ... [-f Script_file] ... [File ...]

# 5  
Old 03-04-2009
Inplace edit is required

Quote:
Originally Posted by amitranjansahu
sed s/"$OSTRING"/"$NSTRING"/g $FILENAME > NEWFILE

Hi Amit,

Thanks for ur reply. The command you have given uses an temp file or intermediate file which i should not use as the file i am going to edit is very huge in MB.
# 6  
Old 03-04-2009
you can redirect the out put to the same file . I am not sure but you can check it with a sample file

sed s/"$OSTRING"/"$NSTRING"/g $FILENAME > $FILENAME
# 7  
Old 03-04-2009
Quote:
Originally Posted by amitranjansahu
you can redirect the out put to the same file . I am not sure but you can check it with a sample file

sed s/"$OSTRING"/"$NSTRING"/g $FILENAME > $FILENAME

You cannot do like tha. B'coz it will delete all the contents in the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
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

Find and Replace Pattern in file

Ok, so how many times have you received this request? I have been looking through the forum for examples and I see the use of tr, awk and sed to perform similar functions but not sure how to use the tools in this scenario and could use a push in the right direction. GOAL: Search for line... (9 Replies)
Discussion started by: djzah
9 Replies

4. 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 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...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

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

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

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

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

find pattern in FILES and replace it ??

Hi 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... (4 Replies)
Discussion started by: tamer
4 Replies
Login or Register to Ask a Question