Replace a string within a file.. with help of positions

I have a huge file with lot of rows... with each row around 400 characters.. with spaces as well..

(e.g)
Line1:
"AC254600606 USDMI000001Anom01130073981 0000000000000.002005040720991231 999 NNNNdir01130073981 dir0113007 MEDE DEF CO Y MR. NN "

I wanted to replace the characters from '12 - 22' in each line..
(i.e) old value " USD"
new value " 1100USD"

Can anyone please help me with this.

Thanks in advance.
Gopi

Try:

perl -pe 's/(?<=.{11}).{11}/    1100USD/' file
1 Like

Hi,

Thanks for the reply.. The code worked.. but i have a lil diffrent reqt...

Actually.. The lines will contain different values...

Characters in between 12 -22 in each line... i m giving an example below...
OLD Values:

"        USD"
"        EUR"
"        JPY"
"     USDUSD"
"     INRINR"
"         UN"

New Values:

"    1100USD"
"    1100EUR"
"    1100JPY"
" 1100USDUSD"
" 1100INRINR"
"     1100UN"

I need to add "1100" just before each of these words within "11 - 22" positions, in each line.... There will be 4 spaces for sure.... but the point is there can be more spaces also.. but i should add "1100" just before the existing word.. and keep the remaining as spaces..
Please help.

Thanks.
Gopi

See if this works for you:

sed 's/    \([^ ]\)/1100\1/' inp_file
1 Like

Thanks a ton for this...
It worked...

sed 's/    \([^ ]\)/1100\1/' File

But i don't understand how it changed exactly in the same position and not in other positions..
Also i have a requirement... where in i have to do such changes in other positions as well...
So please let me know the meaning behind this... as i wanted to replace certain things.. near 150,225 th position's in each line.. in the file..

Thanks in advance..
Gopi

Hi, Gopi

In the future, please take the time to detail the problem in its entirety to the best of your ability in the first post. Doing so will help those that are trying to help you not waste time on suboptimal approaches.

Regards,
Alister

1 Like

As an example to replace four spaces proceeding the next word from column 150 with a value of "8251", you would do this (Note requires sed that supports the -E option):

sed -E 's/^(.{150}.*)    ([^ ])/\18251\2/' infile

Input:

      1         1         1
      4         5         6
...789012345678901234567890123456
---------------------------------
...567484   498.23      EF  funny
...  8484    32.58     FAU  funky
... 14841  1234.60       R  happy

Output:

      1         1         1
      4         5         6
...789012345678901234567890123456
---------------------------------
...567484   498.23  8251EF  funny
...  8484    32.58 8251FAU  funky
... 14841  1234.60   8251R  happy
1 Like

Hi Chubler,

As said.. my sed does not have -E option.
Is there any other way of acheiving the same... May be multiple steps, is also fine..

Thanks in Advance..
Gopi

Well, this sort of thing can be done with awk script but it's probably going to be a little more maintainable if you stick with sed.

The -E option was really just to allow us to say .{150} without -E we are stuck putting 150 dot characters is it's place.

You could do this quite easily in vi with 150i.<ESC>, but this is pretty bad form and I recon it's worth the overhead to build an environment var with this string of 150 dots in it, let's call it D150:

D150=$(print "%150s" "" | tr " " ".")
sed 's/^\('$D150'.*\)    \([^ ]\)/\18251\2/' infile

The above script will work in most shells (except probably csh). If your doing a few of these sorts of things in the 1 script you could even write a function to fill n dots:

function filldot {
    printf "%$1s" "" | tr " " "." 
}
sed 's/^\('$(filldot 150)'.*\)    \([^ ]\)/\18251\2/' infile

Hi,

I tried the code snippet... it gives me diffrent output...
I just had a file with two rows, and uploaded the result....

Actually i'm trying the code for position before 23... but it changes in '475' position... so i did not try for replacing in other positions....

Hope the screenshot helps you to identify the problem..

Thanks,
Gopi

Yes I see the issue, the .* part is matching too much, try this:

sed 's/^\('$D150'[^ ]* *\)    \([^ ]\)/\1!!!!\2/' infile

Hi,

Now a little different behavior... Please see the attached screenshot....
Hope it helps you....

Thanks in Advance
Gopi

This is what the code is supposed to do: from possition 117 search forwards to find next non-space with 4 or more spaces in front of it, and replace the last 4 spaces with 8251.

replaced as below

000000(other-non-spaces)         999
000000(other-non-spaces)     8251999

I assume you wanted to replace 00000000000.00 with 00000008251.00 in which case you would need something like this:

sed 's/^\('$D150'[^0]*0*\)0000\.00/\18251.00/' infile
1 Like

Thanks a lot... For your time.... Made it work...

Gopi