replace a word in a file only first 10 occurances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace a word in a file only first 10 occurances
# 1  
Old 11-12-2008
Bug replace a word in a file only first 10 occurances

file.txt contains
abc :-X 1234 :-X fjhfkf :-X ffwerw :-X fdfgetw :-X hwfhe89 :-X

in the this file there are 6 occurance of -X
i want to replace only first 3 occurances of '-X' with 'XY'
and write back to same file.
# 2  
Old 11-12-2008
Try:

Code:
sed -e 's/X/XY/1' -e 's/X/XY/2' -e 's/X/XY/3' < filename

# 3  
Old 11-12-2008
Thats really quick reply, appreciate the response. I had already tried that, but i want to put it in a loop.
for example there can be 1000 occurances out of which i want to replace only first 500 occurances.

following line inside loop will affect take too much time if the txt file is 100MB
sed -e 's/X/XY/$i'

Please suggest sed or awk program.
# 4  
Old 11-12-2008
Try:
Code:
v=10
awk -vvar=$v 'BEGIN{cnt=1;} { while((match($0,"-X")>0)&& (cnt <=var)){ sub(/-X/,"XY",$0); cnt++;} print; }' < filename

# 5  
Old 11-12-2008
this is awsome, works great....... Thanks buddy.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 Replies

2. UNIX for Advanced & Expert Users

Perl command to replace word in file...

Hi, I want to search for a specific word in file and replace whole line with new text. e.g. 1) I have file with below lines APP=ABCD 12/12/2012 DB=DDB 01/01/2013 I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT Simlarly need a... (2 Replies)
Discussion started by: mgpatil31
2 Replies

3. Shell Programming and Scripting

Replace first 3 occurances of a character in a file for each line

Hi all, I am very new to unix - ksh. I want to replace some characters in a file with some other, but only for first three occurances for each line. For eg: the first 3 occurances character ']' has to be replaced with ',' in each line. Please help. thanks Sreejith (1 Reply)
Discussion started by: rsreejithmenon
1 Replies

4. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

5. Shell Programming and Scripting

Replace 2 Occurances of Space (sed)

I have a large file that looks like the below output: system SUNWxwmod X Window System kernel modules system SUNWxwoft X Window System optional fonts system SUNWxwopt X Window System Optional Clients system ... (1 Reply)
Discussion started by: hxman
1 Replies

6. Shell Programming and Scripting

Replace a word with string from another file

Hi, this belongs a little to my other post but only at the starting point. With find -name "*.htm*" i got a list like this: ./1999/01/file1.html ./1999/01/file2.html ./1999/02/file1.html ./2000/04/file1.html ./2000/04/file2.html ./2000/04/file3.html ./2000/file1.html... (2 Replies)
Discussion started by: Tonda
2 Replies

7. UNIX for Dummies Questions & Answers

Replace word in a file

Guys, I need to replace a portion of the lines in the file depending on the word in my file i have this content use DBNAME go print "use DBNAME" i want to replace the variable DBNAME with something else. But the catch is this line is not always "use DBNAME". Sometimes it can be like "use... (3 Replies)
Discussion started by: sasiharitha
3 Replies

8. Shell Programming and Scripting

replace word in a file

there are 300 files in a directory , some of these files has a word "error" , I word to change this word to "message" , can advise what can i do ? thx. (5 Replies)
Discussion started by: ust
5 Replies

9. Shell Programming and Scripting

Replace all occurances of a string in all file-/foldernames, recursively

I need a script that will replace all occurances of a string in all filenames and foldernames, recursively. Right now I have this script: for f in `find -name *eye*`; do echo processing $f g=`expr "xxx$f" : 'xxx\(.*\)' | tr 'eye' 'm'` mv "$f" "$g" done The problem is that tr... (2 Replies)
Discussion started by: TheMJ
2 Replies

10. Shell Programming and Scripting

How to replace a word with a series of words in a file

Hi, I have a Template file 'TL.body' which says as follows: "There are no <FILENAME> files on the server. " The missing file names are identified and stored in a variable. For Eg: MISSFILE="abc.txt def.txt xyz.txt" I want the values of MISSFILE variable to be replaced against... (2 Replies)
Discussion started by: brap45
2 Replies
Login or Register to Ask a Question