how to search for a str and replace it..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to search for a str and replace it..
# 1  
Old 12-27-2005
how to search for a str and replace it..

in my file, i like to search for a string and if there is a match then i want to replace it with someother string(like in windows wordpad).
i only know how to search for a pattern....[ if( /pattern/ ) ] but i dont know how to replace it..

and
1). i think it is there in unix grep or some other commends... if so.. what is that..
2.) how to do that in perl..
3.)what is good idea... doing that work(finding and replacing a str) in unix shell scripting or perl scripting..
# 2  
Old 12-27-2005
Code:
sed -i -e 's/pattern/replacement/global-flag" input.txt


Last edited by vino; 12-27-2005 at 07:43 AM..
# 3  
Old 12-27-2005
you can open the file in vi and use %s/"old string"/"new string"/g in the command mode.
# 4  
Old 12-27-2005
Quote:
Originally Posted by sekar sundaram
2.) how to do that in perl..
with perl...
eg:

$ cat test
abc def xyz 123 456 789
$ perl -pi -e 's|abc|123|g' test
$ cat test
123 def xyz 123 456 789

to backup the original file use -pi.bak option & your original file will be saved as .bak
# 5  
Old 12-29-2005
"-i " not available...

---------------------------------------------
$ sed -i -e 's/sekar/SEKAR/g' test.log
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]
---------------------------------------------

i am with hp unix B.11.11 U 9000/800
# 6  
Old 12-29-2005
In that case, redirect the output to another file and then rename it.
# 7  
Old 12-29-2005
The -i option is only available with GNU sed.

If you have perl installed at your site inplace substitution is easy - take a look at ashterix's post above.

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. UNIX for Dummies Questions & Answers

Help with search and replace

Hi, I am using vi. I need help with search and replace. I have a file close to 100 lines. I want to search /var and replace with rm /var. /var/sadm/pkg/SUNWcsu/save/125378-03/undo.Z /var/sadm/pkg/SUNWcsr/save/pspool/SUNWcsr/save/127718-02/undo.Z... (4 Replies)
Discussion started by: samnyc
4 Replies

3. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

5. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

6. UNIX for Dummies Questions & Answers

Req 1-liner for Awk, et al to find str position

Hi, I'm trying to find the position of a series of numbers within a large text file. The numbers are separated by spaces. This works fine: type Huge_File.txt | gawk "{print index($0,"255")}" But this does not: type Huge_File.txt | gawk "{print index($0,"188 028 239 160 016 190 137... (4 Replies)
Discussion started by: Lemming42
4 Replies

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

8. UNIX for Dummies Questions & Answers

reg str concat

hi friends. i want to create new file name depends upon user input value.......the format is "txt_VVVV.txt". is it possible? #bin/ksh typeset -i10 i = $1 case $i in *) fil = "txt_000"$i".txt" ;; *) fil = "txt_00" $i... (4 Replies)
Discussion started by: ilayans
4 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

how to get the pos() of 2 str matches in one loop in perl

Hello all i have this simple loop that gets me every time the match of "<#" in my string something like that : my $str ="gggg<#nnnnn#>kkkk<#ssss#>llllll"; while($str =~m/<#/g){ print pos($str); } but now i like to get another pos in the same loop iteration , i will like to get the... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question