OOPS!! I got it slightly wrong. Corrected version at end.
On 19-Mar-10 00:10:09, Ted Harding wrote:
> On 19-Mar-10 05:11:35, Thomas Taylor wrote:
>> I import a comma separated list from my bank and open it in OOcalc. I
>> have trouble reading the date in their format of YYYYMMDDhhmmss, it's
>> all glommed together. I want to run a script on it before importing
>> that will put some character (prefer either "_" or " " as shown below.
>>
>> DEBIT,20100208120000[0:GMT], \
>> DEBIT,20100208120000[0:GMT], \
>> DEBIT,20100204120000[0:GMT], \
>> DEBIT,20100125120000[0:GMT], \
>> DEBIT,20100125120000[0:GMT], / rest of lines trimmed
>> DEBIT,20100122120000[0:GMT], /
>> CREDIT,20100120120000[0:GMT], /
>> CHECK,20100119120000[0:GMT], /
>> ^ ^
>> date field (YYYYMMDDhhmmss)
>>
>> DEBIT,2010_02_08_120000[0:GMT], <<<<< end result I want
>>
>> pass 6 or 7 char then find ","
>> then 4 digits for year
>> insert "_"
>> then 2 digits for month
>> insert "_"
>> then 2 digits for day
>> insert "_"
>> get rest of line
>>
>> Tried combinations of sed & awk but haven't been able to insert the
>> "_"
>> between the year, month, day, and time fields.
>>
>> Pointers and suggestion would be greatly appreciated. I'm in the
>> process of learning bash scripting so please include how the script
>> works (what it does). I'm an old hand at C and assembler but the
>> bash/sed/awk syntax has me baffled.
>> --
>> Thanks, Tom (retired penguin)
>> openSuSE 11.3-M3, kde 4.4.0
>> FF 3.6.0
>
> Try a script based on the following (which you can execute as-is
> from the command line):
>
> awk '
> BEGIN{FS=","} { DT=$2; Y=substr(DT,1,4); M=substr(DT,5,2);
> D=substr(DT,6,2); R=substr(DT,8); };
> {print $1 "_" Y "_" M "_" D "_" R}
> ' << EOT
> DEBIT,20100208120000[0:GMT]
> DEBIT,20100208120000[0:GMT]
> DEBIT,20100204120000[0:GMT]
> DEBIT,20100125120000[0:GMT]
> DEBIT,20100125120000[0:GMT]
> DEBIT,20100122120000[0:GMT]
> CREDIT,20100120120000[0:GMT]
> CHECK,20100119120000[0:GMT]
> EOT
>
>## RESULT:
> DEBIT_2010_02_20_8120000[0:GMT]
> DEBIT_2010_02_20_8120000[0:GMT]
> DEBIT_2010_02_20_4120000[0:GMT]
> DEBIT_2010_01_12_5120000[0:GMT]
> DEBIT_2010_01_12_5120000[0:GMT]
> DEBIT_2010_01_12_2120000[0:GMT]
> CREDIT_2010_01_12_0120000[0:GMT]
> CHECK_2010_01_11_9120000[0:GMT]
>
> Ted.
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <[email protected]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 19-Mar-10 Time: 00:10:06
> ------------------------------ XFMail ------------------------------
awk 'BEGIN{FS=","} { DT=$2; Y=substr(DT,1,4); M=substr(DT,5,2);
D=substr(DT,7,2); R=substr(DT,9); };
{print $1 "_" Y "_" M "_" D "_" R}' << EOT
DEBIT,20100208120000[0:GMT]
DEBIT,20100208120000[0:GMT]
DEBIT,20100204120000[0:GMT]
DEBIT,20100125120000[0:GMT]
DEBIT,20100125120000[0:GMT]
DEBIT,20100122120000[0:GMT]
CREDIT,20100120120000[0:GMT]
CHECK,20100119120000[0:GMT]
EOT
## RESULT:
DEBIT_2010_02_08_120000[0:GMT]
DEBIT_2010_02_08_120000[0:GMT]
DEBIT_2010_02_04_120000[0:GMT]
DEBIT_2010_01_25_120000[0:GMT]
DEBIT_2010_01_25_120000[0:GMT]
DEBIT_2010_01_22_120000[0:GMT]
CREDIT_2010_01_20_120000[0:GMT]
CHECK_2010_01_19_120000[0:GMT]
--------------------------------------------------------------------
E-Mail: (Ted Harding) <[email protected]>
Fax-to-email: +44 (0)870 094 0861
Date: 19-Mar-10 Time: 00:14:54
------------------------------ XFMail ------------------------------