sed command on AIX, replace specific characters

Hi,

Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed.

the source data that I have is

$cat v.txt
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   0000000097950007077Y10001 6022100000     0 0
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   000000009795 367077Y10001 6022100000     0 0
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   0000000097954367077Y10001 6022100000     0 0

I am using the below command which I got from this forum

sed -r -i.Original '/^4000/s/(.{136}) 36|000/\1036/' file

but it is not working on AIX

You are using GNU sed syntax. -i means 'in place' the -i.Original is a suffix to add to a tmp file so you can get your old file back if there was an error. -r is also not standard.

See: sed

You should read the man page for sed on your AIX FIRST before doing copy of code from here.

sed 'pattern and instructions go here'  inputfile >tmpfile
# if no mistakes in tmpfile be sure to check it; then
mv tmpfile inputfile

sorry about that, I actually modified the code above to suit the AIX sed and am using the below code now

$cat v.txt
4000422985400050170315170328000000000006333522000000000173000160000000MS000000652363009301030000800000013111000460021000060000000726710035001200000000000000000000000000
$sed '/^4000/s/\(.\{136\}\)000/\1036/' v.txt
4000422985400050170315170328000000000006333522000000000173000160000000MS000000652363009301030000800000013111000460021000060000000726710035001203600000000000000000000000

what confuses me is why are the characters are positions 143,144 and 145 changing when i think the above AIX sed code only replaces characters at positions 137-139

Hi,

What we forgot to mention in the other thread is that when you use 000 instead of ... then you need to use an anchor for the beginning of the line in the form of a caret ( ^ ), otherwise it will try to match 000 with 136 arbitrary characters before it ..

--
Further, alternation is not part of regular sed (which uses BRE, basic regular expressions), instead try specifying the substitution twice:

sed '/^4000/{s/^\(.\{136\}\)000/\1036/; s/^\(.\{136\}\) 36/\1036/;}' file

---
Alternation would have worked with GNU sed with the -r option (or BSD sed with the -E option) that allows the use of extended regular expressions (ERE), which does have alternation, but GNU sed is usually not present as standard on AIX.
Try:

sed -r '/^4000/s/^(.{136})( 36|000)/\1036/' file
1 Like

You can easily port it to perl

perl -i.Original -pe '/^4000/ and s/(.{136}) 36|000/\1036/' file 
1 Like

thanks will try it out and let you know:b:

I missed something: \1 is certainly a backreference to the ( ) and should become $1 in perl, and needs to be separated from the following text

perl -i.Original -pe '/^4000/ and s/(.{136}) 36|000/${1}036/' file
1 Like

saved me there:rolleyes::b:

AFAIK in perl both $1 and \1 can be used for backreference:

perlre - perldoc.perl.org

@scrutinizer thanks for tip :b: