Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conver text file csv format

Status
Not open for further replies.

baker83

MIS
Aug 22, 2005
1
CA
Hi.
have a text file at aix, need to convert it to csv format, the file is fixed length, know each column attrib., start/end position.

thanks for help
baker
 
And what have you tried so far ?
Where are you stuck ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
BEGIN{
  # Put your column widths below.
  numcols = split("9 5 12 20",widths)
}
{ start = 1
  for (i=1; i<= numcols; i++)
  { printf "%s", csv(substr($0,start,widths[i]))
    printf "%s", ( i<numcols ? "," : "\n" )
    start += widths[i]
  }
}
function csv(s)
{ # Double any double quotes.
  gsub( /"/, "&&", s )
  return "\"" s "\""
}
 
Hi

If you have [tt]gawk[/tt] that is able to work with fixed width fields :
Code:
BEGIN {
  FIELDWIDTHS="9 5 12 20"
}
{
  for (i=1;i<=NF;i++) printf (i>1?"\t":"") $i
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top