Sponsored Content
Top Forums Shell Programming and Scripting Sed command to globally replace xth occurance. Post 302325227 by lowmaster on Saturday 13th of June 2009 07:05:12 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 03:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy