Sed command to globally replace xth occurance.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed command to globally replace xth occurance.
# 1  
Old 06-13-2009
Sed command to globally replace xth occurance.

This should replace the 1st and the 3rd occurance of "the":

sed -e "s/ the / those /1;s/ the / these /3"

This works only by line.

If there is a second "the", but in an other line for example, it counts it as 1st again and replaces it again.

How can I replace "the" 1st and 3rd occurance globally with sed?
# 2  
Old 06-13-2009
Use Perl:

Code:
perl -i.bck -0777 -pe'
  s/\bthe\b/++$c==3?those:$c==1?these:$&/ges
  ' infile

# 3  
Old 06-13-2009
here's a more readable version
Code:
awk '{
 j=0
 for(i=1;i<=NF;i++){
   if ($i == "the"){        
        j++
        if (j==1) {
            $i = "those"
        }else if (j==3){
            $i = "these"
        }     
   }
 }
 print $0   
}
'  file

# 4  
Old 06-13-2009
Quote:
Originally Posted by ghostdog74
[...]
Code:
[...]
            $i = "those"
[...]

Bare in mind that assigning a value to a field will have side effects (FS to OFS).
# 5  
Old 06-13-2009
yes i agree with radoulov on that

Code:
awk '/the/{n+=1}{if(n==1||n==3){sub("the","OK",$0)};print}' filename


Last edited by vidyadhar85; 06-13-2009 at 10:46 PM..
# 6  
Old 06-13-2009
Quote:
Originally Posted by ghostdog74
here's a more readable version
Code:
awk '{
 j=0
 for(i=1;i<=NF;i++){
   if ($i == "the"){        
        j++
        if (j==1) {
            $i = "those"
        }else if (j==3){
            $i = "these"
        }     
   }
 }
 print $0   
}
'  file


this looks absolutely beautiful, and it would be nice to work with, but I end up in this error:


+ awk '{ j=0 for(xi=1;xi<=NF;xi++){ if ($xi == "the"){j++ if (j==1) { $xi = "those" }else if (j==3){ $xi = "these" }}}print $0}'
awk: syntax error at source line 1
context is
{ j=0 >>> for <<< (xi=1;xi<=NF;xi++){ if ($xi == "the"){j++ if (j==1) { $xi = "those" }else if (j==3){ $xi = "these" }}}print $0}
awk: illegal statement at source line 1



However, the perl solution works great...

but how can I add a variable similar like this perl/sed mix (which ofcourse does not work this way):

Code:
perl -pe' s/\bthe\b/++$c==3?those ${kw2} :$c==1?these ${kw1} :$&/ges '



Thanx a lot!

Last edited by lowmaster; 06-13-2009 at 08:56 PM..
# 7  
Old 06-13-2009
Quote:
Originally Posted by lowmaster
this looks absolutely beautiful, and it would be nice to work with, but I end up in this error:


+ awk '{ j=0 for(xi=1;xi<=NF;xi++){ if ($xi == "the"){j++ if (j==1) { $xi = "those" }else if (j==3){ $xi = "these" }}}print $0}'
awk: syntax error at source line 1
context is
{ j=0 >>> for <<< (xi=1;xi<=NF;xi++){ if ($xi == "the"){j++ if (j==1) { $xi = "those" }else if (j==3){ $xi = "these" }}}print $0}
awk: illegal statement at source line 1
why do you want to cramp everything to one line? makes your code ugly to read. Indent your code properly. one liners are cool, but not that cool when you have to troubleshoot your problems.
if you absolutely want to cramp everything to one line, make sure you use appropriate ";".
Code:
awk '{ j=0 ; for (...... }'

-----Post Update-----

Quote:
Originally Posted by vidyadhar85
yes i agree with radoulov on that

Code:
awk '/the/{n+=1}{if(n==1&&n==3){sub("the","OK",$0)};print}' filename

i see a logic problem here. n cannot be both equal 1 AND 3 at the same time. therefore your substitution will not work. also i think OP wants to change the "the"s on a line. If there are multiple "the"s on a line, /the/ will only count as 1. Also, what about resetting back n to 0 ?

while i agree with both of you as well about changing the fields, however, in this simple case, its alright.

Last edited by ghostdog74; 06-13-2009 at 09:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

2. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

3. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

4. Shell Programming and Scripting

replace/delete odd occurance

how to delete/replace the the odd occurance of a pattern say newline character "\n" a,b,c,d e,f g, h, i, j, k, l m,n 1, 2, 3, 4, 5, 6 1, 3, 4, 5, 6, 7 the output should be a,b,c,de,f g, h, i, j, k, lm,n 1, 2, 3, 4,5, 6 1, 3, 4, 5, 6, 7 (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

5. Shell Programming and Scripting

Search and replace only the first occurance

Hi, I need to search a string and replace with nothing, but only the First occurring string using sed/perl/awk (3 Replies)
Discussion started by: suraj.sheikh
3 Replies

6. Shell Programming and Scripting

awk to replace second occurance

#original file . . ~ ~ Index=2 xxx replace #dont replace 1st occurance yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=3 xxx replace (3 Replies)
Discussion started by: cjjoy
3 Replies

7. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

8. UNIX for Dummies Questions & Answers

Replace first 5 occurance of a pattern

There is scenario that i have to replace a pattern at the first 5 occurance in a file. say i need to replace 'abc' by 'xyz' at its first 5 occurance in a file a.txt, can anybody help me how it can be done, i can do complete replacement of the pattern using sed throughtout the file. (1 Reply)
Discussion started by: palash2k
1 Replies

9. UNIX for Dummies Questions & Answers

replace the n'th occurance in a file

Hi All, How can i replace the n'th occurance in a file. ? I have a property file like EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;1;valeur EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;2;valeur2... (2 Replies)
Discussion started by: subin_bala
2 Replies

10. Shell Programming and Scripting

Javascript Replace Globally

I have a bit of javascript function hasSpaces(name) { if(name.search(/^\s*/) != -1){ alert(name); name.replace((/^\s*/, "~")) alert(name); } return name; } ... (2 Replies)
Discussion started by: insania
2 Replies
Login or Register to Ask a Question