text processing - Replace the third comma on each line with newline

I have these long IDs which consist of four sections:

AKJHGFGUIKL,OIUYT,KJHBTYUI,98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL,6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH,34567898766

I want to put the numbers in new line and remove the third comma.

AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567

RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876

ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766

How can I do this?

5 Answers

  1. Jimmy- Reply

    2019-11-14

    awk -F, -vOFS=, '{print $1,$2,$3; print $4; print ""}' file
    

    will produce your desired output

  2. Joe- Reply

    2019-11-14

    A few Perl approaches:

    $ perl -pe 's/,([^,]+)$/\n$1\n/' file
    AKJHGFGUIKL,OIUYT,KJHBTYUI
    98765434567
    
    RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
    6789876
    
    ETRYTUUI,YTYUIL,UIOKJHGFGH
    34567898766
    

    The -p means "read the input file line by line and print each line after applying the script given by -e to it". The s/foo/bar/ is the substitution operator which will replace foo with bar. Here, we are matching a comma followed by one or more non-comma characters ([^,]+) until the end of the line ($). The parentheses around the ([^,]+) will "capture" whatever is matched so we can refer to it as $1 on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.

    If you can't be sure the third comma is the last one, you can do:

    perl -pe 's/([^,]+,){3}\K(.+)/\n$2\n/' file
    

    or

    perl -pe 's/(.+?,.+?,.+?),(.+)/$1\n$2\n/' file
    

    And here are some more, just for fun:

    perl -pe 's/([^,]+,){3}\K(.+)/\n$2\n/' file
    perl -F, -pe '$k=pop(@F); $_=join(",", @F)."\n$k\n"' file
    perl -F, -le 'print join ",", @F[0..2],"\n@F[3..$#F]\n"' file
    
  3. John- Reply

    2019-11-14

    Alternative less succinct sed code:

    sed 's/,\([^,]*\)/\n\1\n/3' file
    

    ...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed syntax) use literal newlines in the RHS:

    sed 's/,\([^,]*\)/\
    \1\
    /3' file
    
  4. Johnny- Reply

    2019-11-14

    awk -F, '{print $1, $2, $3,"\n"$4}'
    
  5. Joseph- Reply

    2019-11-14

    A more portable sed solution:

    <infile sed -E 'G;s/(.*),(.*)(\n)/\1\3\2\3/'
    

    Awk solution(s):

    <infile  awk -F, '{printf("%s,%s,%s\n%s\n\n",$1,$2,$3,$4)}'
    <infile gawk     '{match($0,/(.*),(.*)/,a);print(a[1]"\n"a[2]"\n")}'
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>