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!

Adding number behind variable

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
0
0
US
I have the following command which could not work.
I need to add the number "1" behind the variable.

Code:
	if ($line =~ /(SPEC:)(\w{2})/){
		$line=~s/(SPEC:)(\w{2})/$1$21/;
	}
	print OUT ("$line");
 
input:

xxxxxxx, SPEC:LB,xxxxxx
xxxxxxx, SPEC:M1,xxxxxx

required output:

xxxxxxx, SPEC:LB1,xxxxxx
xxxxxxx, SPEC:M11,xxxxxx
 
actually I am writing this in perl and there are many other lines that follows after this 2 lines that are not in the same format. I just need to change the 1st line. How to implement awk in perl?
 
Hi

omoo said:
I just need to change the 1st line.
Your above example ( 6 Sep 06 6:02 ) shows changes in both line...

To implement [tt]awk[/tt] in [tt]perl[/tt] ? Although [tt]perl[/tt] is capable for almost everything what [tt]awk[/tt] can, they work abit different, so I do not recommend to implement the same operations. While [tt]awk[/tt] automatically reconstructs the record when a field is changed, in [tt]perl[/tt] you have to do it "by hand".
Code:
[gray]# works on lines with SPEC:[/gray]
perl -F, -ape 'if($F[1]=~/SPEC:/){$F[1].="1";$_=join",",@F}' /input/file

[gray]# works on first line[/gray]
perl -F, -ape 'if($.==1){$F[1].="1";$_=join",",@F}' /input/file
And the more [tt]perl[/tt]ish way :
Code:
perl -pe 's/(SPEC:\w+)/${1}1/ if $.==1' /input/file
Please note that you could find better experts and answers for you Perl questions in forum219 ( Perl ).

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top