Operating System - Linux
1755470 Members
3769 Online
108833 Solutions
New Discussion юеВ

How to convert text to csv or xls?

 
SOLVED
Go to solution
Geoff Wild
Honored Contributor

How to convert text to csv or xls?

I have a tab delimited file (in Unix of course) - is there a "free" way to convert it to csv or xls format?

This has to be done at the Unix level - not from Windows...needs to be part of a script...

Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
19 REPLIES 19
Sandman!
Honored Contributor

Re: How to convert text to csv or xls?

Hi Geoff...without a sample input file here's my construct:

# tr "\t" "," outfile

cheers!
Kent Ostby
Honored Contributor

Re: How to convert text to csv or xls?

awk '{FS="\t"; for (idx=1;idx output.csv
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Geoff Wild
Honored Contributor

Re: How to convert text to csv or xls?

Here's part of the file...

Basically, I want to open in Excel (or management does) and not have to do a "data" -> "Text to columns" every time....

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: How to convert text to csv or xls?

This is actually far from trivial. String fields must be enclosed in quotes. Because the string field might contain comma's the comma's need to be quoted as well. You must compose a valid header line.

I would look hard at the Text::CSV Perl module.
If it ain't broke, I can fix that.
someone_4
Honored Contributor

Re: How to convert text to csv or xls?

Hi

I took your data file and imported in excel.
Was this what you where trying to do?

open excel

file
open - pick your file
Deleminited is checked
Next
Tab
Finish.

Richard
James R. Ferguson
Acclaimed Contributor

Re: How to convert text to csv or xls?

Hi Geoff:

Try this:

# perl -pe 's/(.+?)\t+/"$1",/g;s/,([^"]+?)$/,"$1"/' filename

Regards!

...JRF...
Patrice Le Guyader
Respected Contributor

Re: How to convert text to csv or xls?

Hi,

I agree with Clay, I would look hard to Text::CSV Perl module and/or after I would use Spreadsheet::WriteExcel to create this file.

http://search.cpan.org/~alancitt/Text-CSV-0.01/CSV.pm
http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.16/lib/Spreadsheet/WriteExcel.pm

Hope this helps
Kenavo
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Geoff Wild
Honored Contributor

Re: How to convert text to csv or xls?

Richard - yes - that's what I want it to look like - but without having to do in Excel...

Sandmans tr works fine.

Clay - yes - I've heard of that module - through google - I may have to look at it further..

James - yours works as well....

Kent - yours dropped the last column for some reason?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: How to convert text to csv or xls?

Wow - those perl modules are intense!

That is going to take me a wee bit to disect....

Is there a Perl Doctor in the house? :)

Here's my script so far:

#!/bin/sh
#
# EC-Inventory-Data-Collection script
# uses data gathered by /opt/AssetCentre/asset_reporting.ksh
# which is stored in /var/opt/AssetCentre/reports
#
#

RPTDIR=/var/opt/AssetCentre/reports
TMPFIL=/tmp/EC-Inventory-Data.txt
CSV=/htdocs/mambo/dmdocuments/Infrastructure/Server-Planning/EC-Inventory-Data-Collection.csv

if [ -f $CSV ] ;
then
cp -p $CSV $CSV.old
fi

cat $RPTDIR/label > $TMPFIL

for SERVER in `ls $RPTDIR/*.value`
do
cat $SERVER >> $TMPFIL
done

tr "\t" "," <$TMPFIL >$CSV



The SERVER.value (tab delimited) files are created from another script - all I want to do is gather all that individual info and put it in a spreadsheet...

Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.