Search code examples
parsingsedcut

replace a fixed position in a file with a set string


Hello I am looking to do the following:

file= sample.out

replace whatever text is in position 10-15 with "77777" but only on rows that start with ABC

so far I have:

cat sample.out | sed -r 's/^ABC(.{9})/\177777/'

But ABC at the beginning of the lines gets removed and 77777 gets inserted into the text position instead of replacing the existing the characters in that position.


Solution

  • Without example I use this.

    File: repl.txt

    0000000001111111111222222222233333333333
    1234567890123456789012345678901234567890
    ABCDEFGH<12345>IJKLMNOPQRSTUVWXYZ======:
    

    With this command:

    $ sed -e 's/^\(ABC.\{6\}\).\{5\}\(.*\)$/\177777\2/' repl.txt
    0000000001111111111222222222233333333333
    1234567890123456789012345678901234567890
    ABCDEFGH<77777>IJKLMNOPQRSTUVWXYZ======: