LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I convert to fixed length format. (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-convert-to-fixed-length-format-625963/)

rainmk 03-05-2008 02:35 PM

How can I convert to fixed length format.
 
I have a text file delimited by coma. My question is how can I print this file with fixed length of each field so that it looks nicer.
example
this is what i have


05449461986,218923639384,2008-03-04 13:56:02
9054494619863345,218923639384,2008-03-04 13:56:02
27826514080,33675668124,2008-03-05 02:54:23


this is what i want please ignore the - sign as it only represents the space for illustration purposes.

05449461986,--------218923639384,-----2008-03-04 13:56:02
9054494619863345,---218923639384,-----2008-03-04 13:56:02
27826514080,--------33675668124,------2008-03-05 02:54:23

can I do it in awk? is there a way to output each field with a spcified length.

theNbomr 03-05-2008 03:08 PM

In many languages you will find some variant of the printf() function. The format specifiers for most variable types allow you to specify a minimum field width.
You could have preserved the formatting of your example by enclosing it in [c o d e] tags.
Like this
Code:

    printf( "%30s, %30s, %30s\n", oneString, nutherString, annanutherOne );
man printf
--- rod.

Tinkster 03-05-2008 03:10 PM

Hi,

And welcome to LQ!

There sure is.
Code:

awk -F, '{printf("%20s %20s %15s\n", $1, $2, $3)}' file


Cheers,
Tink

Tinkster 03-05-2008 03:26 PM

And a nicer version that doesn't set the width to a fixed one, but
determines the required padding on the fly ;}

Code:

BEGIN{
  FS=","
  max1=0;
  max2=0;
  max3=0
}
{
  line[NR]=$0;
  if(length($1) > max1){max1=length($1)}
  if(length($2) > max2){max2=length($2)}
  if(length($3) > max3){max3=length($3)}
  lines=NR
}
END{
  fmt="%"max1"s,  %"max2"s,  %"max3"s\n"
  for(i=1;i<=lines;i++){
    split( line[i], a)
    printf fmt, a[1], a[2], a[3]
  }
}

Save as script.awk, run like so:
Code:

$ awk -f script.awk fixed
    05449461986,  218923639384,  2008-03-04 13:56:02
9054494619863345,  218923639384,  2008-03-04 13:56:02
    27826514080,  33675668124,  2008-03-05 02:54:23



Cheers,
Tink

rainmk 03-05-2008 03:38 PM

Thanks guys it worked. :)

sofux 05-01-2008 05:02 PM

I have a similar problem but I don't have a fix number of columns.
How can I extend the last code to handle any number of columns specified as a variable?

sachigar 06-09-2011 04:47 AM

I know that this is an old post, but it was usefull to me, so here i let you the code that I created from the last one posted here (Thanks Tinkster ). This should work with any number of fields.

Code:

{
  line[NR]=$0;
  nfields[NR]=NF;
  for(i=1;i<=NF;i++){
    if(length($i) > max[i]){max[i]=length($i)}
  }
  lines=NR
}
END{
  for(j=1;j<=lines;j++){
    split( line[j], a)
    fmt="%"max[1]"s"
    printf fmt, a[1]
    for(i=2;i<=nfields[j];i++){
      fmt=FS"%"max[i]"s"
      printf fmt, a[i]
    }
    printf "\n"
  }
}


Save as script.awk, run like so:

Code:

  $ cat fixed
America;Uruguay;Montevideo
Europa;Alemania;Berlin
Antartida;-;-

  $ awk -F";" -f script.awk fixed
  America; Uruguay;Montevideo
  Europa;Alemania;    Berlin
Antartida;      -;        -

Hopefully this will be useful,
Regards,
Santiago.


All times are GMT -5. The time now is 06:15 AM.