魏长东

weichangdong

东邪

vim&perl对匹配数字做运算

遇到这样个事情,php读取excel的时候,40多列,然后读取后到一个数组,然后key就是0到40多,然后去除每个列的数值。如下代码,但是突然要在最前面或者中间增加一列,然后数组的数字就的变。

$date_preset = $tmp[5];
$country = $tmp[6];
$uiver = $tmp[7];
$user_id = $this->user_id;
$account_id = $tmp[8];

$impressions = isset($tmp[9]) ? $tmp[9] : '';
$click = isset($tmp[10]) ? $tmp[10] : '';
$roi = isset($tmp[11]) ? $tmp[11] : '';
$install = isset($tmp[12]) ? $tmp[12] : '';
$ctr = isset($tmp[13]) ? $tmp[13] : '';
$cvr = isset($tmp[14]) ? $tmp[14] : '';
$cpi = isset($tmp[15]) ? $tmp[15] : '';
$spend = isset($tmp[16]) ? $tmp[16] : '';
$cost = isset($tmp[17]) ? $tmp[17] : '';
$cost_rate = isset($tmp[18]) ? $tmp[18] : '';
$run_days = isset($tmp[19]) ? $tmp[19] : '';
$adset_status = isset($tmp[20]) ? self::statusChange($tmp[20]) : '';
$act_budget = isset($tmp[21]) ? $tmp[21] : '';
$act_bid = isset($tmp[22]) ? $tmp[22] : '';
$act_op_status = isset($tmp[23]) ? self::statusChange($tmp[23]) : '';
$exec_time_month = isset($tmp[24]) ? $tmp[24] : '';
$exec_time_day = isset($tmp[25]) ? $tmp[25] : '';
$exec_time_hour = isset($tmp[26]) ? $tmp[26] : '';
$expire_time = isset($tmp[27]) ? $tmp[27] : '';
$has_sub = $tmp[28];

if ($has_sub) {
	$data_1 = [];
	$date_preset_1 = isset($tmp[29]) ? $tmp[29] : '';
	$country_1 = isset($tmp[30]) ? $tmp[30] : '';
	$data_1['impressions'] = isset($tmp[31]) ? $tmp[31] : '';
	$data_1['click'] = isset($tmp[32]) ? $tmp[32] : '';
	$data_1['roi'] = isset($tmp[33]) ? $tmp[33] : '';
	$data_1['install'] = isset($tmp[34]) ? $tmp[34] : '';
	$data_1['ctr'] = isset($tmp[35]) ? $tmp[35] : '';
	$data_1['cvr'] = isset($tmp[36]) ? $tmp[36] : '';
	$data_1['cpi'] = isset($tmp[37]) ? $tmp[37] : '';
	$data_1['spend'] = isset($tmp[38]) ? $tmp[38] : '';
	$data_1['cost'] = isset($tmp[39]) ? $tmp[39] : '';
	$data_1['cost_rate'] = isset($tmp[40]) ? $tmp[40] : '';
	$data_1['run_days'] = isset($tmp[41]) ? $tmp[41] : '';
	$adset_status_1 = $tmp[42];
}

然后就想吧对应的key都减一。

用vim命令

:%s/\d\+/\=submatch(0)-1/

用perl

perl  -anle '{$_ =~/.*\[(\d+)\].*/;if($1 -neq ''){$wcd=$1-2;s/$1/$wcd/g;}print  $_}' test_jiajian

用sed但是没有实现。

sed -e 's/\[\([0-9]\{1,2\}\)\]/\[\1-1\]/g'  test_jiajian