use SED to replace repeat statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use SED to replace repeat statements
# 1  
Old 09-29-2009
use SED to replace repeat statements

Hi
I need to write a script that read a input file that had same statement repeatedly to replace only 2nd & 5th time repeated statements (ex: This is UNIX forum) with another statement ( UNIX forum threads in Shell programming) with out modifying 1st,3,4th repeated statements. I am planning to do with sed. Any other available better approaches?
# 2  
Old 09-29-2009
Quote:
Originally Posted by watsup
... that read a input file that had same statement repeatedly to replace only 2nd & 5th time repeated statements (ex: This is UNIX forum) with another statement ( UNIX forum threads in Shell programming) with out modifying 1st,3,4th repeated statements.
...
With no clue as to what your data looks like, I shall assume that yours is the simplest case where the string to be replaced occurs all by itself in a line.
Thus -

Code:
$
$ cat -n f3
     1  This is line 1. Some stuff here.
     2  This is UNIX. And some more stuff here. The weather's good.
     3  This is UNIX forum.
     4  Fourth line here.
     5  This is UNIX forum.
     6  And some more.
     7  This is UNIX forum.
     8  The line that does not have the target string.
     9  This is UNIX forum.
    10  Some filler stuff here.
    11  And finally -
    12  This is UNIX forum.
    13  The end.
$
$
$ ##
$ perl -lne 'BEGIN {$from="This is UNIX forum.";
>                   $to="==> UNIX forum threads in Shell programming <=="}
>            if (/$from/) {
>              $i++;
>              if ($i == 2 or $i == 5){print $to} else {print}
>            } else {print}' f3
This is line 1. Some stuff here.
This is UNIX. And some more stuff here. The weather's good.
This is UNIX forum.
Fourth line here.
==> UNIX forum threads in Shell programming <==
And some more.
This is UNIX forum.
The line that does not have the target string.
This is UNIX forum.
Some filler stuff here.
And finally -
==> UNIX forum threads in Shell programming <==
The end.
$
$

tyler_durden
# 3  
Old 10-08-2009
Thanks Tyler..Got it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Help with replace comma with new line and repeat the content

Input file G A G A,T A C A G,T,C T C . . Desired Output file G A G A G T A C A G A T A C T C (4 Replies)
Discussion started by: perl_beginner
4 Replies

3. Shell Programming and Scripting

Sed/replace help

How can we empty or replace with null, following block of code (within the php quotes including the quotes) from inside a file. *** some other data above this code <? #317008# ... (5 Replies)
Discussion started by: fed.linuxgossip
5 Replies

4. Shell Programming and Scripting

Parse through ~21,000 Database DDL statements -- Fastest way to perform search, replace and insert

Hello All: We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following: 1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON... (5 Replies)
Discussion started by: madhunk
5 Replies

5. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

6. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

7. Shell Programming and Scripting

help with sed or replace

Hi, I have to replace the values in a file with the values from my variables. For e.g file contains Prev_date=’01-01-1999’ Curr_date= ’02-11-2010’ I need to replace the value of this prev_date and curr_date with the value of the variables I create namely Last_day=02-10-2010... (3 Replies)
Discussion started by: dsravan
3 Replies

8. Shell Programming and Scripting

using sed to replace help

Hi, i am following content in file cat file Install Installation-path variable Now i need to replace Installation-path with some text to be provided as argument in csh script invocation My question is , can i replace this by only using path eg. sed "s/path/$1" file but it... (1 Reply)
Discussion started by: sarbjit
1 Replies

9. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

10. 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
Login or Register to Ask a Question