Operating System - HP-UX
1826293 Members
4034 Online
109692 Solutions
New Discussion

Re: Using sed to substitute along a line between column positions

 
SOLVED
Go to solution
Dave Walley
Frequent Advisor

Using sed to substitute along a line between column positions

Hi. I need to be able to replace characters between specific column positions along a line, as follows. The first line is the data the second line is the column numbers.

0215-04@10648218001105122@RRRR0000@002000@

123456789012345678901234567890123456789012
1 2 3 4

In this example I would like to substitute the @ in positions 26 & 35 with a 9 from positions 20 40 only. Leaving the @ at posiiton 8 unchanged. Thanks
why do i do this to myself
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Using sed to substitute along a line between column positions

Hi Dave:

If I understand correctly, you want to substitute the "@" character in positions 26 & 35 with a "9". This would work:

# perl -pe 's/(.{25,25})@(.+)/\1_\2/;s/(.{34,34})@(.+)/\1_\2/;s/_/9/g'

Regards!

...JRF...
Dave Walley
Frequent Advisor

Re: Using sed to substitute along a line between column positions

James. Thanks for your reply, the user in question wants to use sed so I was looking for a reply using sed. I will pass on your suggestion in the meantime. Happy christmas to you. Dave
why do i do this to myself
Sandman!
Honored Contributor

Re: Using sed to substitute along a line between column positions

Dave,

Here is the sed approach...

# sed -e 's/@/9/2' -e 's/@/9/2'

cheers!
Kent Ostby
Honored Contributor
Solution

Re: Using sed to substitute along a line between column positions

Here is an awk solution.

create a file called useme.awk

call it as follows:

awk -f useme.awk < data > fixed_data

useme.awk should look like:

{l1=$0;
for (idx1=20;idx1<=40;idx1++)
{if (substr(l1,idx1,1)=="@") {l1a=substr(l1,1,idx1-1);l3a=substr(l1,idx1+1);
l1=l1a"9"l3a;}
}
print l1;

}

best regards,

Oz
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
James R. Ferguson
Acclaimed Contributor

Re: Using sed to substitute along a line between column positions

Hi (again(:

It occurs to me from your description that you might not always want to substitute a "9" character, but rather whatever is in position 20 and 40 into 26 and 35, respectively. This would accomplish that:

# cat my.pl
#!/usr/bin/perl
while (<>) {
$c20=substr($_,20,1);
$c40=substr($_,40,1);
s/(.{25})@(.+)/\1$c20\2/;
s/(.{34})@(.+)/\1$c40\2/;
print;
}

run as:

# my.pl file

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Using sed to substitute along a line between column positions

substr can be an lvalue in perl :)

# perl -pe'substr($_,25,10)=~s/\@/9/g' your_file

will replace all '@' characters in every line from position 26 (perl's offsets start at 0) over a length of 10 into '9's. You can also use tr// and save one character

Demo:
lt09:/home/merijn 112 > cat xx.txt
0215-04@10648218001105122@RRRR0000@002000@
lt09:/home/merijn 113 > perl -pe'substr($_,25,10)=~s/\@/9/g' xx.txt
0215-04@106482180011051229RRRR00009002000@
lt09:/home/merijn 114 > perl -pe'substr($_,25,10)=~tr/@/9/' xx.txt
0215-04@106482180011051229RRRR00009002000@
lt09:/home/merijn 115 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Dave Walley
Frequent Advisor

Re: Using sed to substitute along a line between column positions

Thanks for all your help, I was able to use a number of your suggestions. As always the forum has been a great help. Merry Christmas to one and all. Dave
why do i do this to myself