Inserting a character to a line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinpe
    New Member
    • Aug 2007
    • 7

    Inserting a character to a line

    Hi,

    How do I insert character (e.g. , or .) at certain positions in a line? For example I want to insert a period ( . ) after the first 4 characters of every each line in the file. I cannot do it in sed command. I'm playing with my script using perl command but to no avail. Please advice. Thanks in advance.

    See below:

    Input:
    Code:
    12345
    67890
    23456
    78901
    34567
    Desired Output:
    Code:
    1234.5
    6789.0
    2345.6
    7890.1
    3456.7

    Br,
    Pete
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Here is one way you can do it.

    [CODE=perl]my $string = 12345;
    $string =~ s/^(\w{4})(.*)$/$1.$2/g;

    print $string;[/CODE]

    [CODE=perl]#Output:
    1234.5[/CODE]
    --Kevin

    Comment

    • pinpe
      New Member
      • Aug 2007
      • 7

      #3
      Hi,

      I found my answer. This works on me:

      Code:
      $x =~ s/\A(....)/$1./;
      Tnx anyway. =)

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by pinpe
        Hi,

        I found my answer. This works on me:

        Code:
        $x =~ s/\A(....)/$1./;
        Tnx anyway. =)

        Where did you find it?

        Comment

        Working...