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.
# 8  
Old 06-13-2009
Quote:
Originally Posted by ghostdog74
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-----

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, 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.
... I know I know. I need a quick solution for a problem and did not digg deeper into all those fantastic sed/awk/perl stuff...

you are absolutely right, oneliners are very very bad for reading.

You should see my code... nearly all pocessing is done in one line, a lynx output piped again and again... kilometers long.

And the more readable version of it made me even understand what this onliners really are...

Now this AWK solution of ghostdog74 works allmost perfect, if I put the ; to the right places, but

Quote:
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.
I need this to count the "the"s of all lines, this solution seems to count and replace only per line, like my simple sed example at the beginning...

Thanx alot! Smilie

Last edited by lowmaster; 06-13-2009 at 11:05 PM..
# 9  
Old 06-13-2009
Quote:
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.
Sorry I did a typing mistake its not && it should be ||
thanks for pointing it out Smilie
# 10  
Old 06-14-2009
Quote:
Originally Posted by lowmaster
.

I need this to count the "the"s of all lines, this solution seems to count and replace only per line, like my simple sed example at the beginning...

Thanx alot! Smilie
ah...so its not on each line, but one all lines .... my bad.
# 11  
Old 06-14-2009
Quote:
[...]
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 -i.bck -0777 -pe'BEGIN {
        ( $v1, $v2 ) = ( $ENV{kw1}, $ENV{kw2} );
    }
    s/\bthe\b/
        ++$c == 3 ?
      "those $v2 " :
        $c == 1    ? 
        "these $v1 " :
          $&
      /xges
  ' infile

# 12  
Old 06-14-2009
Quote:
Originally Posted by radoulov
Code:
perl -i.bck -0777 -pe'BEGIN {
        ( $v1, $v2 ) = ( $ENV{kw1}, $ENV{kw2} );
    }
    s/\bthe\b/
        ++$c == 3 ?
      "those $v2 " :
        $c == 1    ? 
        "these $v1 " :
          $&
      /xges
  ' infile

Thanx for this, but my $v1, $v2 stays empty for some reason. What have I to do to to move the content of kw1 to v1?

Code:
part1=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)
part2=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part2.TXT)
part3=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part3.TXT)
part4=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part4.TXT)
part5=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part5.TXT)
part6=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)
part7=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)
part9=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)
part8=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part4.TXT)
...
kw1=`echo "$kwt $part9 $kwt $part2 - $kwt - $part6 | $kwt $part8 - $kwt"`
...
...
perl -i.bck -0777 -pe'BEGIN {
        ( $v1, $v2 ) = ( $ENV{kw1}, $ENV{kw2} );
    }
    s/\bthe\b/
        ++$c == 3 ?
      "those $v2 " :
        $c == 1    ? 
        "these $v1 " :
          $&
      /xges
  ' infile


Last edited by lowmaster; 06-14-2009 at 01:05 PM..
# 13  
Old 06-14-2009
You need to export them:

Code:
kw1=...
kw2=...
export kw1 kw2

# 14  
Old 06-14-2009
works like a charm!

Thanx!
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