replacing characters is string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wassssup
    New Member
    • Oct 2007
    • 47

    replacing characters is string

    hi i would like to know how to replace the 2nd/3rd... character in a string without knowing what value it holds.
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    You did not give to much info. Can you use sed or similiar tools?

    Comment

    • wassssup
      New Member
      • Oct 2007
      • 47

      #3
      sorry...i canot use sed or awk..
      for example : if i do
      $ expr index "string" n
      i'll get 5, as n is the fith char, so how do i replace the 5th char?

      Comment

      • rski
        Recognized Expert Contributor
        • Dec 2006
        • 700

        #4
        So you can use only expr?

        Comment

        • wassssup
          New Member
          • Oct 2007
          • 47

          #5
          i can use anything..the only 2 thing i cant use is sed and awk

          Comment

          • rski
            Recognized Expert Contributor
            • Dec 2006
            • 700

            #6
            You mean something like that?

            Code:
            echo -n `expr substr string 1 1`;echo -n TR; expr substr string 4 `expr length\ string`
            it replaces 'tr' to 'TR' in 'string' word.

            Comment

            • wassssup
              New Member
              • Oct 2007
              • 47

              #7
              when u put ; does it mean | can u give an example? i dont really understand the code..im new to unix :)

              Comment

              • wassssup
                New Member
                • Oct 2007
                • 47

                #8
                i dont want to specify the char i want to replace, the the location of the char that i want changed

                Comment

                • rski
                  Recognized Expert Contributor
                  • Dec 2006
                  • 700

                  #9
                  I know I do not specify the char i replace but positions of them.
                  In my resolution I
                  1) print first character
                  2) print next two character want to appear in the string
                  3) print the last part of the string

                  Comment

                  • wassssup
                    New Member
                    • Oct 2007
                    • 47

                    #10
                    i see, so what is the 1 1 in your code? and does it have to be a ` ` there? what does it do

                    Comment

                    • rski
                      Recognized Expert Contributor
                      • Dec 2006
                      • 700

                      #11
                      get the first character of the string (we want to replace 2nd and 3rd character so the 1st character must rewrite)
                      Code:
                      expr substr string 1 1

                      write 'TR' as 2nd and 3rd characters
                      Code:
                      echo -n TR
                      write the rest of the string (character from 4th ). if we don't know what is the the lenght of the string we can use length as here.
                      Code:
                      expr substr string 4 `expr length\ string`
                      ` ` means treat the string inside as a command and execute it.

                      I advise you to read manual for exec command

                      Comment

                      Working...