gonbravo
gonbravo

Reputation: 31

Dealing with date formats in unix

The following sql code will be inserted in a text file, as the input to the input.txt:

insert into t_agg_sgsn_gprs_usage
select
    served_imsi as imsi,
    served_msisdn as msisdn,
    NVL(cell_identity,'-1') as cell_id,
    NVL(trim(to_char(location_area_code,'99999999')),'UNKNOWN'),
    substr(served_imei,1,8) tac_code,
    to_char(to_timestamp(record_opening_time,'YYYYMMDDHH24MISS'),'YYYYMMDD')
        as call_dt,
    'ajay' as loaded_dt,
    count(1) as cdr_count,
    sum(data_volume_uplink) as uploaded_data_volume,
    sum(data_volume_downlink) as downloaded_data_volume,
    case
        when substr(imsi,6,2)='65'
        then substr(imsi,1,9)
        else substr(imsi,1,5)
    end imsi_prefix
from
    t_cdrc_sgsn_pdp_18
    where loaded_dt LIKE 'ajay%' AND trim(served_imsi) != ''
        AND trim(served_msisdn) != '' AND trim(served_imei) != ''
    group by imsi, msisdn, tac_code, cell_id, location_area_code, call_dt;

I'm writing a unix script which should give the output with the date incremented by 1, and appended to one sql file(changer.sql), till the specified date.

insert into t_agg_sgsn_gprs_usage
select
        served_imsi as imsi,
        served_msisdn as msisdn,
        NVL(cell_identity,'-1') as cell_id,
        NVL(trim(to_char(location_area_code,'99999999')),'UNKNOWN'),
        substr(served_imei,1,8) tac_code,
        to_char(to_timestamp(record_opening_time,'YYYYMMDDHH24MISS'),'YYYYMMDD') as call_dt,
        '2012-10-17' as loaded_dt,
        count(1) as cdr_count,
        sum(data_volume_uplink) as uploaded_data_volume,
        sum(data_volume_downlink) as downloaded_data_volume,
        case when substr(imsi,6,2)='65' then substr(imsi,1,9) else substr(imsi,1,5) end imsi_prefix
from
        t_cdrc_sgsn_pdp_18
        where loaded_dt LIKE '2012-10-17%' AND trim(served_imsi) != '' AND trim(served_msisdn) != '' AND trim(served_imei) != ''
        group by imsi, msisdn, tac_code, cell_id, location_area_code, call_dt;

insert into t_agg_sgsn_gprs_usage
select
        served_imsi as imsi,
        served_msisdn as msisdn,
        NVL(cell_identity,'-1') as cell_id,
        NVL(trim(to_char(location_area_code,'99999999')),'UNKNOWN'),
        substr(served_imei,1,8) tac_code,
        to_char(to_timestamp(record_opening_time,'YYYYMMDDHH24MISS'),'YYYYMMDD') as call_dt,
        '2012-10-18' as loaded_dt,
        count(1) as cdr_count,
        sum(data_volume_uplink) as uploaded_data_volume,
        sum(data_volume_downlink) as downloaded_data_volume,
        case when substr(imsi,6,2)='65' then substr(imsi,1,9) else substr(imsi,1,5) end imsi_prefix
from
        t_cdrc_sgsn_pdp_18
        where loaded_dt LIKE '2012-10-18%' AND trim(served_imsi) != '' AND trim(served_msisdn) != '' AND trim(served_imei) != ''
        group by imsi, msisdn, tac_code, cell_id, location_area_code, call_dt;

And keep on incrementing and appending into the changer.sql file, till the specified enddate.

Below is what I have so far, but that is still no producing the expected result:

startdate = `date +"%y-%m-%d"`
set startdate =  2012-10-17
enddate = `date “+%y-%m-%d` 
set enddate = 2012-12-22
for($startdate <= $enddate)
do
for file in $(grep -il "ajay" input.txt)
do
sed -e "s/ajay/$startdate/ig" $file >> changer.sql
done
$startdate --startdate="-1 days ago"
done

Upvotes: 3

Views: 322

Answers (1)

Rubens
Rubens

Reputation: 14768

Considering your problem concerns bash only, you have the following errors:

1 - what would you do this for:

startdate=...
set startdate=...

2 - your syntax is wrong for assignments in bash; you should not have whitespaces between:

rvalue=lvalue
startdate=`date +"%y-%m-%d"`

3 - this syntax is wrong for a for loop; guess you wanted a while loop:

for ($startdate <= $enddate)  *wrong*
do ...
done

while (( $startdate <= $enddate )); do
    ...
done

4 - what are you expecting from:

$startdate --startdate="-1 days ago"

you may be trying to decrease the value of $startdate by 1 day, for what you'd have to do this:

startdate=$(date -d $startdate' -1 days' +"%y-%m-%d")

Upvotes: 3

Related Questions