Can sed replace every 2 instances it finds in a file? Pattern.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can sed replace every 2 instances it finds in a file? Pattern.
# 8  
Old 03-06-2010
Sure:
Code:
awk '/PORT/ && gsub("PORT",int(i)){i+=0.5}1' file

# 9  
Old 03-06-2010
Sweet, you sir, are a god...

Can you tell me what does this do?

"int(i)){i+=0.5}1"

What can I change if I want it to do every 3 instances or maybe start like at 4000 for the integer?
# 10  
Old 03-06-2010
I believe you want to replace each pair of mentions of PORT with a unique port number, the following does that:
Test file before running script:
Code:
$ cat port_test
1:PORT
2:PORT
3:PORT
4:PORT
5:PORT
6:PORT
7:PORT
8:PORT
9:PORT
11:PORT
12:PORT
13:PORT
14:PORT
15:PORT
16:PORT
17:PORT
18:PORT
19:PORT
21:PORT
22:PORT
23:PORT
24:PORT
$

Test file after running script:
Code:
$ cat port_test
1:1000
2:1000
3:1001
4:1001
5:1002
6:1002
7:1003
8:1003
9:1004
11:1004
12:1005
13:1005
14:1006
15:1006
16:1007
17:1007
18:1008
19:1008
21:1009
22:1009
23:1010
24:1010
$

Script to do the above:
Code:
$ cat port_test.sh
index=1000
STARTLINE=1
ENDLINE=`expr ${STARTLINE} + 1`
while [ ${index} -lt 1500 ]; do
  sed -e ''${STARTLINE}','${ENDLINE}'{
s/PORT/'${index}'/ 
}' port_test > port_test.$$ && \
  cp port_test.$$ port_test
  index=`expr ${index} + 1`
  STARTLINE=`expr ${STARTLINE} + 2`
  ENDLINE=`expr ${STARTLINE} + 1`
done
rm port_test.$$
$

This should work in Bourne and BASH shells.
Increase the value 1500 to reflect the number of lines to be changed in your file, in my example's case it could have been set to 1011.

HTH.
# 11  
Old 03-06-2010
Quote:
Originally Posted by EXT3FSCK
Sweet, you sir, are a god...

Can you tell me what does this do?

"int(i)){i+=0.5}1"

What can I change if I want it to do every 3 instances or maybe start like at 4000 for the integer?
Explanation:
Code:
gsub("PORT",int(i))

substitute all instance of PORT in the current line with the integer of i
Code:
{i+=0.5}1

increase i with 0.5 and print the line

This approach suites for 2 instances:

Quote:
int(0) gives 0
int(0.5) gives 0
int(1) gives 1
int(1.5) gives 1
etc.
If you don't have to much substitutes, you can try some thing like for 3 instances if you want to start with 4000:

Code:
awk -v i="4000" '/PORT/ && gsub("PORT",int(i)){i+=0.33334}1' file

# 12  
Old 03-06-2010
Nicely done, Franklin52.

I took the liberty of building on your solution and parameterizing it. i=starting port number, n=how many times to reuse a port number:
Code:
awk '/PORT/ && gsub("PORT",i){i+=!(++j%n)}1' i=1 n=2 file

Regards,
Alister

Last edited by alister; 03-06-2010 at 07:44 PM..
# 13  
Old 03-06-2010
Quote:
Originally Posted by alister
Nicely done, Franklin52.

I took the liberty of building on your solution and parameterizing it. i=starting port number, n=how many times to reuse a port number:
Code:
awk '/PORT/ && gsub("PORT",i){i+=!(++j%n)}1' i=1 n=2 file

Regards,
Alister
Thanks Alister, this did the trick perfectly.

Thank you, I can't thank you enough.
# 14  
Old 03-07-2010
Quote:
Originally Posted by alister
Nicely done, Franklin52.

I took the liberty of building on your solution and parameterizing it. i=starting port number, n=how many times to reuse a port number:
Code:
awk '/PORT/ && gsub("PORT",i){i+=!(++j%n)}1' i=1 n=2 file

Regards,
Alister
I had something similar to that in mind with a mod operator but with NR:
Code:
awk '/PORT/ && gsub("PORT",i){i+=!(NR%n)}1' i=4000 n=3 file

Regards

Last edited by Franklin52; 03-07-2010 at 08:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Beginners Questions & Answers

sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line. Example: Input: ( two lines of file) Name: 'text1:200/text2:1.2.3.4' Name2:... (19 Replies)
Discussion started by: Beginner101
19 Replies

3. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

4. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

sed finds nothing but it changes file's timestamp

I must go through some files to change a certain string within text files to another string. I use openSUSE and folders are mounted by cifs. Text to be replaced (only in .m extension) is U:\FOLDER and new string is N: That works fine with spaces in directory names etc., but this process ... (5 Replies)
Discussion started by: Pappa41
5 Replies

6. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

7. UNIX for Dummies Questions & Answers

Using sed command to remove multiple instances of repeating headers in one file?

Hi, I have catenated multiple output files (from a monte carlo run) into one big output file. Each individual file has it's own two line header. So when I catenate, there are multiple two line headers (of the same wording) within the big file. How do I use the sed command to search for the... (1 Reply)
Discussion started by: rebazon
1 Replies

8. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 Replies

9. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question