I have a data file (see sample three lines below) separated by pipes.
What I want to be able to do is to "chomp" out the fields and put them into variables. They are of pre-defined lengths. I want to turn the file into an SQL script as per:
Input:
| 416|CAWNBORE LIMITED |CAWNBORE | 8| 0| 0|00.00 | 0| 0|********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* | 0| 0| 0| 0| 0|
| 431|MAIN HOLDINGS LIMITED |MAINHOLDINGSCHA | 8| 0| 0|00.00 | 0| 0|********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* |********NO ADDRESS DETAILS******* | 0| 19650509| 0| 0| 0|
| 432|DUBLIN NORTH CITY MILLING COMPANY LIMITED |DUBLINNORTHCITY | 8| 0| 1|00.00 | 18750125| 19830124|113 PHIBSBORO ROAD |DUBLIN | | | 216410| 19901106| 0| 20030124| 0|
Basically, strip the pipes - separate the data with commas and insert the SQL string INSERT INTO .... VALUES( data_in_here comma separated );
Desired output:
INSERT INTO tbcrocompany (id_company, nm_company, id_keyword, cd_status, dt_company_status, cd_type, cd_principle_obj, dt_register, dt_last_ar, ad_line_1, ad_line_2, ad_line_3, ad_line_4, cd_town_number, dt_dissolved, dt_bond_expiry, dt_next_ar, dt_last_accounts) VALUES (416,'CAWNBORE LI|MITED','CAWNBORE',8,0,0, '00.00', 0, 0, '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', 0, 0, 0, 0, 0);
INSERT INTO tbcrocompany (id_company, nm_company, id_keyword, cd_status, dt_company_status, cd_type, cd_principle_obj, dt_register, dt_last_ar, ad_line_1, ad_line_2, ad_line_3, ad_line_4, cd_town_number, dt_dissolved, dt_bond_expiry, dt_next_ar, dt_last_accounts) VALUES (431,'MAIN HOLDIN|GS LIMITED','MAINHOLDINGSCHA',8,0,0, '00.00', 0, 0, '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', '********NO ADDRESS DETAILS*******', 0, 19650509, 0, 0, 0);
INSERT INTO tbcrocompany (id_company, nm_company, id_keyword, cd_status, dt_company_status, cd_type, cd_principle_obj, dt_register, dt_last_ar, ad_line_1, ad_line_2, ad_line_3, ad_line_4, cd_town_number, dt_dissolved, dt_bond_expiry, dt_next_ar, dt_last_accounts) VALUES (432,'DUBLIN NORTH CITY MILLING COMPANY LIMITED','DUBLINNORTHCITY',8,0,1, '00.00', 18750125, 19830124, '113 PHIBSBORO ROAD', 'DUBLIN', '', '', 216410, 19901106, 0, 20030124, 0);
,
but there are no commas in your sample data. Please clarify exactly what you are trying to do... – jasonwryan Mar 24 '16 at 21:09while IFS=","
towhile IFS="|"
your script snippet should work. Although'$c5'
will output $c5 literally, not the value of variablec5
– MelBurslan Mar 24 '16 at 21:11awk
. You will be able to do this job much, much easier becauseawk
is actually designed for text processing.bash
is not. – Wildcard Mar 24 '16 at 21:19xxxxxxxx
I can't tell which part you want to end up where. (Use names of fruits or "example.com" or whatever.) – Wildcard Mar 24 '16 at 21:23echo "Some text and '$c5'"
will correctly expand$c5
. The single quotes aren't special within double quotes. – Wildcard Mar 24 '16 at 21:28gcc
is designed for text processing, because C++ source code is stored in a text file. Look at the post I linked to earlier; it has a great discussion of the conceptual purpose of a shell. – Wildcard Mar 25 '16 at 00:42