Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2004
    Posts
    46

    Post Unanswered: comma delimited in a script

    Hi

    I have a following requirement

    input
    -----
    sdkfjggs
    skdk
    tueods
    jdkflghfff

    ouput
    ----
    s,d,k,f,j,g,g,s
    s,k,d,k
    t,u,e,o,d,s
    j,d,k,f,l,g,h,f,f,f

    after certain number of characters in each line(1 character in this case) i should put a comma delimiters... can any one help me do this using script...

    Thanks
    Mark.

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    sed -e 's/./&,/g;s/,$//g' file

  3. #3
    Join Date
    Jun 2004
    Posts
    46
    Thanks for your input
    can you explain me what does your command does and also if i have to put comma after 2 chars where should i change.

    Thanks
    Mark

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    ok, a bit of a different spin - if you need to change the 'starting' location.

    nawk -v start=2 -f mark.awk file

    you can change 'start' from the command line - to start from the 5-th char:

    nawk -v start=5 -f mark.awk file

    here's mark.awk
    Code:
      # determine the length of a record/line
      len=length($0);
      # print chars. UP to the 'start' location
      printf("%s%s", substr($0,1,start-1), (start >= len) ? "\n" : "");
    
      # loop thorugh the remaning chars (up to 'len') putting ',' in-between
      for(i=start; i <= len; i++)
         printf("%s%s", substr($0,i,1), (i==len) ? "\n" : ",");
    }

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    can you explain me what does your command does and also if i have to put comma after 2 chars where should i change.
    If you mean a comma after evey 2 chars, stick with the sed solution...

    sed 's/../&,/g;s/,$//' yourFile > newFile

    The '.' is a wildcard representing a single character, the '&' represents the pattern matched matched by the regular expression - in this example '..', i.e. any 2 characters. Overall, you are replacing every 2 characters with those same 2 characters plus a comma (and then you replace the comma that would be at the end of line with 'nothing' to remove it).

  6. #6
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    I think the OP wanted to start placing commas AFTER a certain number of characters; say after 3 chars:

    sdkfjggs

    would become:

    sdk,f,j,g,g,s

  7. #7
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Try this :

    I="marks_inputfile"
    O="marks_outputfile"
    D="," # Delimiter to use
    hop_len="3" # length of the chars you want to split
    > $O

    cat $I | awk -v hop_len=$hop_len -v O=$O -v D=$D ' BEGIN { FS="@"}
    {
    str_len=length($1)
    x=1
    comma_string=""
    while ( x < str_len )
    {
    help_var=substr($1,x,hop_len)
    if (x == 1 )
    {
    comma_string=help_var
    }
    else
    {
    comma_string=comma_string""D""help_var
    }
    x=x+hop_len
    }
    # output on screen
    print comma_string
    # output in a file
    print comma_string > O
    }'
    # output from the new file to screen
    # cat $O
    Last edited by fla5do; 06-23-04 at 16:43.
    Greetings from germany
    Peter F.

  8. #8
    Join Date
    Jun 2004
    Posts
    46
    Thanks guys, for your replies...

  9. #9
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Quote Originally Posted by markjason
    Thanks guys, for your replies...
    which solution you prefer ?
    I am interested !
    Greetings from germany
    Peter F.

  10. #10
    Join Date
    Jun 2004
    Posts
    46
    Hard to say as per my req as of now i would prefer
    sed -e 's/./&,/g;s/,$//g' file

    Thanks
    Mark.

Posting Permissions

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