Replacing text on every third line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text on every third line
# 1  
Old 04-22-2012
Replacing text on every third line

I have file like this

"copy table_name from filea.txt on node replace delimiter '|';"
"copy table_name from fileb.txt on node replace delimiter '|';"
"copy table_name from filec.txt on node replace delimiter'|';"
"copy table_name from filee.txt on node replace delimiter '|';"
"copy table_name from filed.txt on node replace delimiter '|';"
"copy table_name from filef.txt on node replace delimiter '|';"
"copy table_name from fileg.txt on node replace delimiter '|';"
"copy table_name from fileh.txt on node replace delimiter '|';"
"copy table_name from filei.txt on node replace delimiter '|';"
"copy table_name from filej.txt on node replace delimiter '|';"
"copy table_name from filek.txt on node replace delimiter '|';"
"copy table_name from filel.txt on node replace delimiter '|';"
.
.
.
.
100000 lines

I want to change the above pattern to
"
"copy table_name from filea.txt on node1 delimiter '|';"
"copy table_name from fileb.txt on node2 delimiter '|';"
"copy table_name from filec.txt on node3 delimiter'|';"
"copy table_name from filee.txt on node1 delimiter '|';"
"copy table_name from filed.txt on node2 delimiter '|';"
"copy table_name from filef.txt on node3 delimiter '|';"
"copy table_name from fileg.txt on node1 delimiter '|';"
"copy table_name from fileh.txt on node2 delimiter '|';"
"copy table_name from filei.txt on node3 delimiter '|';"
"copy table_name from filej.txt on node1 delimiter '|';"
"copy table_name from filek.txt on node2 delimiter '|';"
"copy table_name from filel.txt on node3 delimiter '|';"
.

I am trying to use sed command on this as follows

for i in {1..3}
do
sed -e"s/replace/$i/g" output.txt >> final_output.txt
done

but the above code gives me every single file with node 1 node , node 3 command i.e. for each copy command there are three copy command generated.

Please help me with this.
# 2  
Old 04-22-2012
Try this:

Code:
awk '{ sub( "node", "node" ((NR-1)%3)+1, $0 ); print; }' input-file >output-file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add previous text when replacing comma with new line

Hi, I've got this output: # cat test2.txt TM1ITP1-TMNLSTP1 SLC00=0,SLC01=0,SLC02=0,SLC03=0 if I just use cat test2.txt | tr "," "\n" I'll end up very near to what I'm trying to achieve: TM1ITP1-TMNLSTP1 SLC00=0 SLC01=0 SLC02=0 SLC03=0 But how can i eventually add the term... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Checking and replacing first line in text file, one-liner?

Hello, I'm looking to check only the first line of a file to see if it is a format string, like # -*- coding: utf-8; mode: tcl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -\*- vim:fenc=utf-8:ft=tcl:et:sw=2:ts=2:sts=2if the first line is anything else, insert the above string. I'd... (3 Replies)
Discussion started by: f77hack
3 Replies

3. Shell Programming and Scripting

[Solved] Replacing line of text while file is closed

Is it possible to replace a line of text within a file while it's closed with a single command or a script? Please show me an example or point me to a webpage that shows an example. The file has this line of text: LoginGraceTime 100 I want to replace it with the following: ... (2 Replies)
Discussion started by: wdg74
2 Replies

4. Shell Programming and Scripting

Replacing text in Perl given by command line

Hi I need to write a Perl script that the file given as first argument of the command line that will find all occurrences of the string given as the third argument of the command line and replace with the string given as the fourth argument. Name newfound file is specified as the second... (3 Replies)
Discussion started by: nekoj
3 Replies

5. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

6. Shell Programming and Scripting

Matching and replacing text

Im new to using perl scripting, and i was wondering if anyone could help me, I need to create a cgi file that when it runs it opens a file looks through all the words, and replace the word hello with goodbye, ive been looking around and someone said to use an If statement, but ive found no other... (2 Replies)
Discussion started by: xzen123
2 Replies

7. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies

8. Shell Programming and Scripting

Replacing text

I was using the following code to replace the path names and it works when it is echo "$PWD/$f" | sed -e 's/^.*chris\.domain\.com/chris.domain.com/' IN fact it works great However I tried to incorporate a variable echo "$PWD/$f" | sed -e... (3 Replies)
Discussion started by: chrchcol
3 Replies

9. Shell Programming and Scripting

replacing text

hey how can i change part of a file i hve to do in a masses so mv or cp is not practical. I have to change xxx_rrr to xxx_yyy pls help thank (2 Replies)
Discussion started by: ajaya
2 Replies

10. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies
Login or Register to Ask a Question