Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sub function 1

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
0
0
CO
Hi

I want to change a especific position into register.

In my programm I have to use four lines to do this.

part_one=substr($0,1,10)
part_two="2"
part_three=substr($0,12)
$0=parte1""parte3""parte4


I only want to change the character number 11 into the register

is it possible to do this using sub funtion.??

sub(regex,replacement,target)

sub(/3/,"2",substr($0,11,1)) I used this and it wasnot possible.



Thanks


malpa
 
Hi

Could you please be careful with your sample codes ? The variable names are completely messed.

The only shorter solution I know uses the capturing feature of the [tt]gensub()[/tt] function's regular expression, which is [tt]gawk[/tt] extension :
Code:
awk --re-interval '{print gensub(/(.{10})./,"\\12","")}' /input/file

Feherke.
 
Or:

Code:
new=substr($0,1,10) "2" substr($0,12)

In normal awks you may not be able to assign something to $0.

Annihilannic.
 
Hi

[tt][small][blue][ignore][offtopic][/ignore][/blue][/small][/tt]
Or maybe alternatives...
Code:
sed 's/./2/11' /input/file

[gray]# or[/gray]

perl -pe 'substr($_,10,1)="2"' /input/file

[gray]# or[/gray]

ruby -pe '$_[10]="2"' /input/file
[tt][small][blue][ignore][/offtopic][/ignore][/blue][/small][/tt]


Feherke.
 
Hi

Or another alternative with [tt]awk[/tt], but still no [tt]sub()[/tt] :
Code:
awk -F '' -vOFS='' '$11="2"' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top