sed returns different results while substitution on a pipe delimited file

Hi,

Need help with a sed command that I am using to substitute 3 positions of a pipe delimited file.
i am getting different results while substituting the same position of two different files with the same value. Please see details below:

 
$ cat chk2
 
0008086172|0000000003|0000007468|MX|0000001003|0904867492|00029|00001|03000|00001|2011-07-25 18:38:47|A|00911|0000537851|0000001003|0052138123|||00005|0000|02501|0006914405||006914404|000088563128875|00002|001088563128875||||A|2011-07-13|2011-08-01|2049-12-31|2011-07-15|2011-07-13||00033|0000||00090|N|N|N|N|0002||||N||||||DM||KG|||00000025776|00000029900|EA|000010000|
EA|||N|Y|Y|||||N/A|N/A|H|Y|000002500|N|00001|00006|||||BH|00120|00001|0003|N|N|0000001933200|C|0000000001|00000010000|KG|N|002150000|001600000|000510000|CM|754399583|LT|0|||00006|00005|0000001933200|0000000001|002150000|001600000|754399583|LT|000510000|CM|00000010000|KG|0000000000||R|00001|Y||0263965052|00005|00002|MX|0005|100|||||+|00005|+|00030|+|00005|+|00030||||N|Y|N|||||||||||N|3533|0019||2011-07-13|0005||||||N|N|0005||||00000010000|KG|Y|||N|0000||0001||||||||||N|N|||N|N|N|N||
 
$ cut -f 99,100,101 -d '|' chk2
 
002150000|001600000|000510000
 
$ sed -e 's/|/000010000|/99' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/100' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/101' -e 's/\(.*\)|.*000010000/\1|000010000/' chk1| cut -f 99,100,101 -d '|'
 
002150000000010000|001600000000010000|000510000000010000

As seen above, i am replacing the 99th,100th,101th position of the pipe delimited file using the sed command to 000010000, but looks like it is appending 000010000 to each values.

But when i am doing the same thing on the same position for another file, it's replacing correctly:

 
$ cat chk2
0000336853|0000000003|0000007088|CA|0000001002|0039424552|00029|00001|03000|00001|2011-07-23 23:02:08|A|00908|0002887394|0000001002|0052248160|||00028|0000|00208|0010575385||010575384|000003600014264|00002|001003600014264|00003|001003600014264||A|2010-12-02|2010-12-31|2049-12-31|2010-12-03|2010-12-02||00020|0000||00090|N|N|N|N|0002||||N||||||IN||LB|||00000004397|00000004397|EA|001640000|EA|||N|Y|Y|||||||H|Y|000026685|N|00001|00000||00003|||HA|00120|00001|0003|N|N|0000000415400|C|0000000001|00000057607|KG|N|000015900|000008875|000016800|IN|000001371|CF|0|||00006|00005|0000000415400|0000000001|000015900|000008875|000001371|CF|000016800|IN|00000057607|KG|0000000000||R|00001|Y||0857622280|00028|00000|US||100|||||+|00035|+|00080|+|00035|+|00080||||N|N|N|||||||||||N|0516|0002||2010-12-02|||||||N|N|0028|0000|2010||00000127000||Y|||N|0000||0001||||||||||N|N|||N|N|N|N||
 
$ cut -f 99,100,101 -d '|' chk2
 
000015900|000008875|000016800
 
$ sed -e 's/|/000010000|/99' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/100' -e 's/\(.*\)|.*000010000/\1|000010000/' -e 's/|/000010000|/101' -e 's/\(.*\)|.*000010000/\1|000010000/' chk2 | cut -f 99,100,101 -d '|'
 
000010000|000010000|000010000

Here, the 99th,100th and 101th position is replaced with 000010000.
Just wondering why it is not updated in the first file.?
Anything wrong with the sed command that's used?
Any help is appreciated.

Your "sed" will replace the "|" with "000010000" on the 99 position:

Example:
Data before (123 is field 99):

...|123|456...

Running your "sed":

 sed -e 's/|/000010000|/99'

Data after your "sed":

...|12300010000|456...

You can do it in "sed" using a data mark:

sed -e 's/|/MyMark/99' -e 's/\(.*|\).*MyMark/\1||/' Input_File

The above will remove the value in the field 99.

But I still don't understand why the second example replaces the 99th,100th and 101th position.

My requirement is to replace n positions of a pipe delimited file with a value i specify.
The example you provided is to delete right?
Is there a way to accomplish this?

Thanks in advance,
Vijay

The only "data mark" needed is already provided, the field delimiter, "|":

sed 's/[^|]*|/|/99'

---------- Post updated at 09:42 AM ---------- Previous update was at 09:31 AM ----------

To replace the value of the nth field in a pipe-delimited line, where n=99:

sed 's/[^|]*|/NEWVALUE|/99'

That will not work for the final field since it is not followed by a pipe. If that case needs to be handled, use a '$' anchor instead of a '|' at the end of the regular expression.

Regards,
Alister