Add character at certain position in line

9,054

You can certainly do this with sed but I know perl better ...

  perl -p -i -e 's/^(.{12})/$1 /' $INFILE

Later

  sed -i -e 's/^.\{12\}/& /' $INFILE
Share:
9,054

Related videos on Youtube

aland
Author by

aland

Updated on September 18, 2022

Comments

  • aland
    aland over 1 year

    I've encountered the following problem: I need to add space at certain position in each line, to transform data from

    ATOM      1 HT1 GLY     5      10.346  30.927 130.252  0.00  0.00
    

    to (by adding space in 12th column)

    ATOM      1  HT1 GLY     5      10.346  30.927 130.252  0.00  0.00
    

    Now I've managed to achieve it with:

    cat $INFILE | cut -c-11 > $INFILE.1
    cat $INFILE | cut -c12- > $INFILE.2
    paste -d ' ' $INFILE.1 $INFILE.2 > $INFILE
    

    But may be there is more elegant solution, without using temporary files?

    Thanks in advance.

  • Daniel Andersson
    Daniel Andersson almost 12 years
    sed also knows about {}, but needs escaping: sed -i 's/.\{12\}/& /' $INFILE` works.