LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2008, 02:35 PM   #1
rainmk
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Rep: Reputation: 0
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.
 
Old 03-05-2008, 03:08 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 03-05-2008 at 03:09 PM.
 
Old 03-05-2008, 03:10 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi,

And welcome to LQ!

There sure is.
Code:
awk -F, '{printf("%20s %20s %15s\n", $1, $2, $3)}' file


Cheers,
Tink

Last edited by Tinkster; 03-05-2008 at 03:11 PM. Reason: Heh ... too slow ;}
 
Old 03-05-2008, 03:26 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 03-05-2008, 03:38 PM   #5
rainmk
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks guys it worked.
 
Old 05-01-2008, 05:02 PM   #6
sofux
LQ Newbie
 
Registered: May 2008
Posts: 1

Rep: Reputation: 0
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?
 
Old 06-09-2011, 04:47 AM   #7
sachigar
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
  


Reply

Tags
fixed, length


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
How to convert rm format music files to mp3 format me4linux Linux - Software 2 05-15-2007 01:45 PM
gawk, comma delim to fixed length? johniccp Programming 1 01-20-2005 04:21 PM
problems reading in fixed-length record file naijaguy Programming 1 08-24-2004 02:34 PM
csv to fixed-length file roballen Programming 0 03-11-2004 03:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration