Replace fixed strings only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace fixed strings only
# 1  
Old 06-18-2009
Replace fixed strings only

Hi All,

I just need to do find and replace in a file....
say for eg I have the input file like below:
in.txt
#####
oldtextoldtext
oldtext
oldtext
oldtext
oldtext123
oldtext-
oldtext

I need to replace oldtext to newtext... my output file should come like below..
out.txt
######
oldtextoldtext
newtext
newtext
newtext
oldtext123
oldtext-
newtext

Pls help me....
Thanks in advance
Saravana
# 2  
Old 06-18-2009
what have you tried? put in some effort.
# 3  
Old 06-18-2009
Did you try anything?
# 4  
Old 06-18-2009
I tried with this cmd :
sed -e 's/oldtext/newtext/g' $file

but it is repalcing all the lines... :-(
# 5  
Old 06-18-2009
easy

sed 's/^oldtext$/newtext/g' $file
# 6  
Old 06-18-2009
Sorry... I forgot add this line in my input file.... If I have the below line in my file then it will not work....

something something oldtext

So it will not work.....
# 7  
Old 06-18-2009
Quote:
Originally Posted by pbillast
sed 's/^oldtext$/newtext/g' $file
there's no need for 'g' as the pattern will appear only ONCE.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file. I achieved this using following command: sed 's/\(.\)/\1@/7;s/@\(...\)/ /' FileData.txt > FileData_UPDATED.txt Another situation comes when I need to done same but excluding 1st... (5 Replies)
Discussion started by: manishdivs
5 Replies

2. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

3. Shell Programming and Scripting

Replace and Increment a value in the fixed length file

Hi Everyone, I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this. Ex input file ASDSD ADSD 00000 X AAASD ADSD 00000 X SDDDD ADSD 00000 X Ouput ASDSD ADSD 00001 X AAASD ADSD 00002 X SDDDD ADSD 00003 X ... (7 Replies)
Discussion started by: saratha14
7 Replies

4. Shell Programming and Scripting

To replace the value of the column in a fixed width file

I have a fixed with file with header & trailer length having the same length of the detail record file. The details record length of this file is 24, for Header and Trailer the records will be padded with spaces to match the record length of the file Currently I am adding 3 spaces in header... (14 Replies)
Discussion started by: ginrkf
14 Replies

5. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== IXTTIV110827 NANTH AM IKSHIT ABCDEF 0617 IJAY NAND EENIG ZXYWVU 0912 AP OOK OONG PQRSTU100923 NASA DISH TTY ASDFG 0223 GHU UMA LAM QWERT 0111 ATHE SH THEW ======= From 7th to 12 is a date... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

6. Shell Programming and Scripting

sed to replace at fixed location

I have got a text file- each line of 200 characters length. The file is too large in size. It could be 100 MB. The lines begin with any of 0,1,2,3,4,5. I want to replace from 121-131 characters with spaces irrespective of wehatever it is there (the exisitng charatcers could be spaces). And this I... (6 Replies)
Discussion started by: asutoshch
6 Replies

7. Shell Programming and Scripting

Replacing variable Text between fixed strings

Hello all, This is my first post and I hope you can help me out. I searched for quite some hours now and haven't found a simple solution to my problem. It is as following: I got this file: dl.dropbox.com/u/14586156/stuff/Bookmarks.plist and want to replace the Text between... (9 Replies)
Discussion started by: pasc
9 Replies

8. Shell Programming and Scripting

Search and replace particular characters in fixed length-file

Masters, I have fixed length input file like FHEAD0000000001XXXX20090901 0000009000Y1000XXX2 THEAD000000000220090901 ITM0000109393813 430143504352N22SP 000000000000RN000000010000EA P0000000000000014390020090901 TTAIL0000000003000000 FTAIL00000000040000000002 Note... (4 Replies)
Discussion started by: bittoo
4 Replies

9. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

10. Shell Programming and Scripting

sed replace with fixed length

$ cat template s.noName $ sed "s/s.no/1/" template > out $ sed "s/s.no/100/" template >>out $ cat out 1Name 100Name 1 Name 100Name (7 Replies)
Discussion started by: McLan
7 Replies
Login or Register to Ask a Question