gpt4 book ai didi

linux - 在 unix 中处理日期格式

转载 作者:太空狗 更新时间:2023-10-29 11:09:53 26 4
gpt4 key购买 nike

下面的sql代码将被插入到一个文本文件中,作为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;

我正在编写一个 unix 脚本,它应该给出日期递增 1 的输出,并附加到一个 sql 文件(changer.sql),直到指定的日期。

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;

并继续递增并追加到 changer.sql 文件中,直到指定的结束日期。

以下是我目前所拥有的,但仍然没有产生预期的结果:

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

最佳答案

考虑到你的问题只涉及 bash,你有以下错误:

1 - 你会做什么:

startdate=...
set startdate=...

2 - 你在 bash 中赋值的语法是错误的;之间不应有空格:

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

3 - 此语法对于 for 循环是错误的;我猜你想要一个 while 循环:

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

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

4 - 你对什么有期望:

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

您可能试图将 $startdate 的值减少 1 天,因为您必须这样做:

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

关于linux - 在 unix 中处理日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com