The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find and replace and keep Celvin VK Shell Programming and Scripting 3 04-03-2009 02:34 PM
awk find/replace RobertSubnet Shell Programming and Scripting 2 03-08-2009 01:45 PM
find and replace javeed7 Shell Programming and Scripting 1 04-02-2008 10:00 AM
find and replace rakshit Shell Programming and Scripting 4 01-24-2008 03:52 AM
find and replace valhutch UNIX for Dummies Questions & Answers 4 07-29-2006 06:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2009
treadwm treadwm is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like...

Host cmg-iqdrw3p4
LocalForward *:9043 localhost:9043
Host cmg-iqdro3p3a
LocalForward *:10000 localhost:10000
Host cmg-iqdro3p3b
LocalForward *:10000 localhost:10000
......

And I want to convert it to:

Host cmg-iqdrw3p4
LocalForward *:9043 localhost:2062
Host cmg-iqdro3p3a
LocalForward *:10000 localhost:2063
Host cmg-iqdro3p3b
LocalForward *:10000 localhost:2064
.......

I used sed to convert 'localhost:xxxx' to 'localhost:'. In vi, I used the following to add back the incrementing port number.

:let i=2000 | g/localhost:/s//\="localhost:".i/ | let i=i+1

Is there a way to do it all in sed? Sed, awk, perl or shell script answers would all be welcome too. Thanks.
  #2 (permalink)  
Old 04-21-2009
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
I'm assuming the counter starts from 2000 ...

Code:
awk -F: '/localhost/{ $NF=i++ }1' i=2000 OFS=: file
  #3 (permalink)  
Old 04-21-2009
treadwm treadwm is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
Talking

that works awesome!
Thanks Rubin.

Quote:
Originally Posted by rubin View Post
I'm assuming the counter starts from 2000 ...

Code:
awk -F: '/localhost/{ $NF=i++ }1' i=2000 OFS=: file
Closed Thread

Bookmarks

Tags
sed awk bash shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0