Sponsored Content
Top Forums Shell Programming and Scripting Sed command to globally replace xth occurance. Post 302325294 by lowmaster on Sunday 14th of June 2009 11:58:29 AM
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..
 

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
code(n) 							    [incr Tcl]								   code(n)

NAME
code - capture the namespace context for a code fragment SYNOPSIS
code ?-namespace name? command ?arg arg ...? DESCRIPTION
Creates a scoped value for the specified command and its associated arg arguments. A scoped value is a list with three elements: the "@scope" keyword, a namespace context, and a value string. For example, the command namespace foo { code puts "Hello World!" } produces the scoped value: @scope ::foo {puts {Hello World!}} Note that the code command captures the cur- rent namespace context. If the -namespace flag is specified, then the current context is ignored, and the name string is used as the namespace context. Extensions like Tk execute ordinary code fragments in the global namespace. A scoped value captures a code fragment together with its namespace context in a way that allows it to be executed properly later. It is needed, for example, to wrap up code fragments when a Tk widget is used within a namespace: namespace foo { private proc report {mesg} { puts "click: $mesg" } button .b1 -text "Push Me" -command [code report "Hello World!"] pack .b1 } The code fragment associated with button .b1 only makes sense in the context of namespace "foo". Furthermore, the "report" procedure is private, and can only be accessed within that namespace. The code command wraps up the code fragment in a way that allows it to be executed properly when the button is pressed. Also, note that the code command preserves the integrity of arguments on the command line. This makes it a natural replacement for the list command, which is often used to format Tcl code fragments. In other words, instead of using the list command like this: after 1000 [list puts "Hello $name!"] use the code command like this: after 1000 [code puts "Hello $name!"] This not only formats the command cor- rectly, but also captures its namespace context. Scoped commands can be invoked like ordinary code fragments, with or without the eval command. For example, the following statements work properly: set cmd {@scope ::foo .b1} $cmd configure -background red set opts {-bg blue -fg white} eval $cmd configure $opts Note that scoped commands by-pass the usual protection mechanisms; the command: @scope ::foo {report {Hello World!}} can be used to access the "foo::report" proc from any namespace context, even though it is private. KEYWORDS
scope, callback, namespace, public, protected, private itcl 3.0 code(n)
All times are GMT -4. The time now is 07:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy