Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    6

    Unanswered: sed......the stream editing....or awk maybe

    encounter problem again -_-b
    i want to insert some character into specific position in a line, for example
    insert a char after the 4th char, or insert a char in front of the 2nd field.etc.........
    how can these be done then......
    thanks for ur attention.......

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    Line="Txt: for test"
    
    #
    # Insert string after the 4th char
    #
    
    echo "$Line" | \
    sed 's/^\(.\{4\}\)/\1(sed after 4th char)/'
    
    echo "$Line" | \
    awk '{print substr($0,1,4) "(awk after 4th char)" substr($0,5)}'
    
    #
    # Insert string before field 2
    #
    
    echo "$Line" | \
    awk '{$2 = "(awk before field2)" $2;print $0}'
    and the result

    Txt:(sed after 4th char) for test
    Txt:(awk after 4th char) for test
    Txt: (awk before field2)for test
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    6

    Cool

    thanks

  4. #4
    Join Date
    Feb 2004
    Posts
    7

    How to do this?

    I have a bunch of codes (like 80), every code has several subroutines. The task I was posed is to add C preprocessing to each subroutine (add #ifdef before each subroutine). I think this can be done using awk script. Could anybody help me on this? Please send email to julytli01@yahoo.com, thanks a lot!
    Originally posted by aigles
    Code:
    Line="Txt: for test"
    
    #
    # Insert string after the 4th char
    #
    
    echo "$Line" | \
    sed 's/^\(.\{4\}\)/\1(sed after 4th char)/'
    
    echo "$Line" | \
    awk '{print substr($0,1,4) "(awk after 4th char)" substr($0,5)}'
    
    #
    # Insert string before field 2
    #
    
    echo "$Line" | \
    awk '{$2 = "(awk before field2)" $2;print $0}'
    and the result

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Do you want to add and '#ifdef' statement only ?
    You do you identify a subroutine ?

    Give us an example of input code and requested result.
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •