Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: converting txt to csv and formatting

by gurpreetsingh13 (Scribe)
on Jul 07, 2014 at 06:03 UTC ( [id://1092520]=note: print w/replies, xml ) Need Help??


in reply to converting txt to csv and formatting

Something out of the way though, but I would say you can try this if you have Vim.

1.Simply make a copy of the file and open it in Vim
2.Use this search-and-replace to surround all names with quotes.
%s/\(.*\),\(.*\)/"\1,\2"/g
3. Again a search and replace to put comma at the end of each line.
%s/\n/,\r/g
4.Then record a simple Macro or make it manually as:
:let @q="JJj"
5. Replay the macro N number of times where N >= records or you can say N = (No. of lines/3)
6. Finally remove the commas at the end of each line with the command:
%s/,$//g
Save the file and you are done. Just rename it to <Filename>.csv and the file is ready.
You can probably emit some steps above if you are good at regex by putting comma in first search-and-replace only at the end of names and phone numbers.
Or making a comma in macro itself.
But better forget all that and this simple one would work I believe.
Hope that solves the problem.

Replies are listed 'Best First'.
Re^2: converting txt to csv and formatting
by gurpreetsingh13 (Scribe) on Jul 07, 2014 at 09:17 UTC
    Update:
    This simple regex would also work under vim instead of using macro.
    s/\(.*\),\(.*\)\n\(.*\)\n\(.*\)\n/"\1,\2",\3,\4\r/g
    Without using vim, under Unix, you can do:
    perl -e 'my $data =`cat test.pl`; $data=~s/(.*),(.*)\n(.*)\n(.*)\n/"\1 +,\2",\3,\4\n/g;print $data;'>newfile.csv
    Under windows, you can do:
    perl -e 'my $data =`type test.pl`; $data=~s/(.*),(.*)\n(.*)\n(.*)\n/"\ +1,\2",\3,\4\n/g;print $data;'>newfile.csv

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-19 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found