shell scripts relating removing leading characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creeds
    New Member
    • Feb 2008
    • 14

    shell scripts relating removing leading characters

    hello,
    I have some problems regarding trimming the leading character

    awk -F"|" '{print $1"|"$2"|"$3"|3 |"$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1
    After getting the file $File-9898.txt.edt1, i am in need to limit the $4 parameter to 14 length so i want to trim everything if that extends more than 14.
    so, how can i proceed,

    with regards
    creeds
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Originally posted by creeds
    hello,
    I have some problems regarding trimming the leading character

    awk -F"|" '{print $1"|"$2"|"$3"|3 |"$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1
    After getting the file $File-9898.txt.edt1, i am in need to limit the $4 parameter to 14 length so i want to trim everything if that extends more than 14.
    so, how can i proceed,

    with regards
    creeds

    use printf instead of print with awk,and specify the format specifier for last field.
    Somthing like below:

    awk -F"|" '{printf "%s|%s|%s|3|%.1 4s\n" ,$1,$2,$3,$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1

    everything will be redirected to $File-9898.txt.edt1 file.
    Pay attention for two things.
    1: ".14" number, which is field width specifier.
    2: \n at the end of string..I am not sure about this..check out yourself.

    Comment

    • creeds
      New Member
      • Feb 2008
      • 14

      #3
      [QUOTE=ashitpro]
      thanks for the solution..
      yeah i also cud dothat using substr(), so that i can count 14, but if i want to cout 14 from the last , what patterns i have to apply ...
      how to modify the following lines which limits 4th file dto first 14th limit
      awk -F"|" '{printf "%s|%.14s|%s|3| %s\n" ,$1,$2,$3,$4}'

      with regards,
      Creeds

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        [QUOTE=creeds]
        Originally posted by ashitpro
        thanks for the solution..
        yeah i also cud dothat using substr(), so that i can count 14, but if i want to cout 14 from the last , what patterns i have to apply ...
        how to modify the following lines which limits 4th file dto first 14th limit
        awk -F"|" '{printf "%s|%.14s|%s|3| %s\n" ,$1,$2,$3,$4}'

        with regards,
        Creeds
        Thats what I said in my previous post..
        Put the '.14' between '%' and 's'.
        same as you've done for printing $2
        So modified commandshould look like:

        awk -F"|" '{printf "%s|%.14s|%s|3| %.14s\n" ,$1,$2,$3,$4}'

        Now your last field will have only 14 characters..

        Comment

        • creeds
          New Member
          • Feb 2008
          • 14

          #5
          [QUOTE=ashitpro][QUOTE=creeds]
          sorry yar,
          i got ur answer and the pattern in the very first reply so no problem with any field being limited to 13 or 14 wotsever...
          its understandable,
          but inthe last post i wanted to count 14 from the last,
          i mean +00977984136612 8 i want to remove + 00 as i limit my parameter to 14 from last....so how to proceed,
          just sort my problem out man,
          with regards,
          Suman

          Comment

          • ashitpro
            Recognized Expert Contributor
            • Aug 2007
            • 542

            #6
            [QUOTE=creeds][QUOTE=ashitpro]
            Originally posted by creeds
            sorry yar,
            i got ur answer and the pattern in the very first reply so no problem with any field being limited to 13 or 14 wotsever...
            its understandable,
            but inthe last post i wanted to count 14 from the last,
            i mean +00977984136612 8 i want to remove + 00 as i limit my parameter to 14 from last....so how to proceed,
            just sort my problem out man,
            with regards,
            Suman
            check out this, modify according to you

            awk -F"|" '{printf "%s|%.14s|%s|3| %s\n" ,$1,$2,$3,subst r($4,length($4)-13,length($4))} '

            Note:If you substract 13 it will print 13+1 characters in last field(ofcourse counting from last character)

            Comment

            • creeds
              New Member
              • Feb 2008
              • 14

              #7
              [QUOTE=ashitpro][QUOTE=creeds][QUOTE=ashitpro]

              hey buddy
              thanks that was clear n clever logic
              regards,
              creeeds

              Comment

              Working...