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. |
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 ); --- rod. |
Hi,
And welcome to LQ! There sure is. Code:
awk -F, '{printf("%20s %20s %15s\n", $1, $2, $3)}' file Cheers, Tink |
And a nicer version that doesn't set the width to a fixed one, but
determines the required padding on the fly ;} Code:
BEGIN{ Code:
$ awk -f script.awk fixed Cheers, Tink |
Thanks guys it worked. :)
|
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? |
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:
{ Save as script.awk, run like so: Code:
$ cat fixed Regards, Santiago. |
All times are GMT -5. The time now is 06:15 AM. |