Use awk to print each character as its column?

I need a reorganization of a large CSV file. The first column, which is currently a six-digit number, needs to be split using commas as the field separator.

For example, I need the following:

022250,10:50 AM,274,22,50
022255,11:55 AM,275,22,55

turned into this:

0,2,2,2,5,0,10:50 AM,274,22,50
0,2,2,2,5,5,11:55 AM,275,22,55

Let me know what you think!

Thank!

+3
source share
4 answers

This is much shorter in perl:

perl -F, -ane '$,=","; print split("",$F[0]), @F[1..$#F]' <file>

perl, . -F, , (, awk). -a ( @F), -n while (<>) { ... }, . -e , - script . $, - ( , , ). split , , /. print, , , .

awk:

awk -F, '{n=split($1,a,""); for (i=1;i<=n;i++) {printf("%s,",a[i])}; for (i=2;i<NF;i++) {printf("%s,",$i)}; print $NF}' <file>
+3

, . split ( , ) , .

  BEGIN{ FS="," }
  {
     n = split( $1, a, "" );
     for ( i = 1; i <= n; i++ )
        printf("%s,", a[i] );

     sep = "";
     for ( i = 2; i <= NF; i++ )
        {
        printf( "%s%s", sep, $i );
        sep = ",";
        }
     printf("\n");
  }
+2

awk

$ awk -F"," '{gsub(".",",&",$1);sub("^,","",$1)}1' OFS="," file
0,2,2,2,5,0,10:50 AM,274,22,50
0,2,2,2,5,5,11:55 AM,275,22,55
+2

Here is a variation of the theme. It should be noted that it prints the remaining fields without using a loop. Another is that since you are iterating over the characters in the first field anyway, why not just do it without using the divide by zero function for split () (which might not be available on some AWK versions):

awk -F, 'BEGIN{OFS=","} {len=length($1); for (i=1;i<len; i++) {printf "%s,", substr($1,i,1)}; printf "%s", substr($1,len,1);$1=""; print $0}' filename

Like a script:

BEGIN {FS = OFS = ","}
{
    len = length($1); 
    for (i=1; i<len; i++)
        {printf "%s,", substr($1, i, 1)}; 
    printf "%s", substr($1, len, 1)
    $1 = "";
    print $0
}
+1
source

Source: https://habr.com/ru/post/1740230/


All Articles