免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4065 | 回复: 6
打印 上一主题 下一主题

关于正则替换求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-11 22:44 |只看该作者 |倒序浏览
本帖最后由 sumin 于 2014-06-11 22:47 编辑

请教下大家正则替换应该如何写的?

需要替换的文件内容如下, 每行都是固定格式的字符串,想批量将第三列"Attachments" 中包含 ...\*.zip 的文件路径删除的,如:


"33813952","CQ00259520","C:\1\new\33813952\ci_mm.h,C:\1\new\33813952\log_csq_failed _on_V12RC2.zip","Updated ci_mm.h file, "
替换为
"33813952","CQ00259520","C:\1\new\33813952\ci_mm.h","Updated ci_mm.h file, "


完整待替换文件如附件的 AT_CMD_attachments.rar (9.47 KB, 下载次数: 4)


"dbid","display_name","Attachments","Description"
"33813948","CQ00259516","C:\1\new\33813948\CI PS Service Group.pdf,C:\1\new\33813948\ARME causes.xlsx,C:\1\new\33813948\CI MM Service Group.pdf,C:\1\new\33813948\CI CC Service Group.pdf,C:\1\new\33813948\ci_mm.h"," , , , ,ci_mm.h"
"33813950","CQ00259518","C:\1\new\33813950\AT$CCLK.xlsx,C:\1\new\33813950\CCLK.zip,C:\1\new\33813950\$CCLK_BUGS.zip,C:\1\new\33813950\AT$CCLK_PASS.zip"," , , , "
"33813951","CQ00259519","C:\1\new\33813951\CI MM Service Group.pdf,C:\1\new\33813951\ci_mm.h,C:\1\new\33813951\13340 51-at-t-spec.pdf,C:\1\new\33813951\ATV-Log 27-1-14 16;15;59.zip"," ,ci_mm.h ? interface file updated,AT&T spec, "
"33813952","CQ00259520","C:\1\new\33813952\ci_mm.h,C:\1\new\33813952\log_csq_failed _on_V12RC2.zip","Updated ci_mm.h file, "
"33813955","CQ00259523","C:\1\new\33813955\AT&F.xlsx,C:\1\new\33813955\AT&F.zip,C:\1\new\33813955\ATampersandF_TC1 30-03-14 07;34;59.zip,C:\1\new\33813955\ATampersandF_TC2 30-03-14 07;34;59.zip"," , , , "
"33813956","CQ00259524","C:\1\new\33813956\ATampersandS.zip,C:\1\new\33813956\AT&S.xlsx"," , "
"33813958","CQ00259526","C:\1\new\33813958\ATstarALS.xlsx,C:\1\new\33813958\ALS_PASS.zip"," , "
"33813962","CQ00259530","C:\1\new\33813962\PrefRat_PS441_2.7z,C:\1\new\33813962\ci_dev.h,C:\1\new\33813962\BAND_FAIL_Log 01-Apr-14 12;55;03.zip"," ,ci_dev.h, "
"33813963","CQ00259531","C:\1\new\33813963\BANDIND_FAIL_ATV-Log 26-03-14 10;10;08.zip"," "
"33813976","CQ00259544","C:\1\new\33813976\ATstarCFUN_FAILED_ATV-Log 26-03-14 12;28;57.zip"," "
"33813980","CQ00259548","C:\1\new\33813980\ATstarCHECKEMER_BUGS.zip,C:\1\new\33813980\ATstarCHECKEMER.xlsx"," , "
"33813981","CQ00259549","C:\1\new\33813981\CIIND.docx","1"
"33813983","CQ00259551","C:\1\new\33813983\Log 12-Mar-14 11;04;29.zip"," "
"33813984","CQ00259552","C:\1\new\33813984\starCNMA_FAIL.zip,C:\1\new\33813984\ATstarCNMA.xlsx"," , "
"33813985","CQ00259553","C:\1\new\33813985\CNTI fail.txt,C:\1\new\33813985\ci_mm.h,C:\1\new\33813985\CI MM Service Group.pdf,C:\1\new\33813985\CNTI_FAIL_ATV-Log 20-2-14 9;44;46.zip,C:\1\new\33813985\ATstarCNTI.xlsx"," ,CI_MM,H, , , "
"33813986","CQ00259554","C:\1\new\33813986\ATstarCPBC_BUG.zip,C:\1\new\33813986\ATstarCPBC.xlsx"," , "
"33813990","CQ00259558","C:\1\new\33813990\cprst.zip"," "
"33813991","CQ00259559","C:\1\new\33813991\Log 29-Oct-13 07;32;41.zip,C:\1\new\33813991\Log 29-Oct-13 10;16;24.zip,C:\1\new\33813991\ATstarCSCB.xlsx,C:\1\new\33813991\CSCB_FAIL.zip"," , , , "
"33813993","CQ00259561","C:\1\new\33813993\ATstarCSIMSTAT.docx,C:\1\new\33813993\CI SIM Service Group.pdf,C:\1\new\33813993\CSIMSTAT_Log 20-Aug-13 11;20;24.zip,C:\1\new\33813993\asteriskCSIMSTAT.xlsx"," , , , "
"33813994","CQ00259562","C:\1\new\33813994\ATstarDEMERGNCY_BUGS.zip,C:\1\new\33813994\ATstarDEMERGNCY.xlsx"," , "
"33814005","CQ00259573","C:\1\new\33814005\ATstarHSPACONFIG.docx,C:\1\new\33814005\Log 18-Feb-14 15;24;35.zip"," , "
"33814006","CQ00259574","C:\1\new\33814006\Log 30-Sep-13 13;46;04.zip,C:\1\new\33814006\Log 01-Oct-13 13;04;37-Restart-Target.zip,C:\1\new\33814006\Log 01-Oct-13 14;19cfun=0cfun=1;04.zip,C:\1\new\33814006\ATstarICCID.xlsx,C:\1\new\33814006\Log_10-Oct-13_11;04;49.zip,C:\1\new\33814006\ICCID_BUG.zip"," , , , ,ACAT log from customer with failure - V9, "
"33814013","CQ00259581","C:\1\new\33814013\MRD system016.pdf,C:\1\new\33814013\ATstarMRDunderscoreAUTH.xlsx"," , "
"33814017","CQ00259585","C:\1\new\33814017\MRD system016.pdf,C:\1\new\33814017\ATstarMRDunderscoreIMEI.xlsx,C:\1\new\33814017\ATstarMRDunderscoreIMEI_TC4 FAIL - ERROR.zip,C:\1\new\33814017\ATstarMRDunderscoreIMEI_TC4 FAIL - SYNTAX.zip,C:\1\new\33814017\V14.1-RC1- ATstarMRDunderscoreIMEI_TC4 FAIL.zip"," , , , , "
"33814018","CQ00259586","C:\1\new\33814018\ATstarMRDunderscoreMEP_TC4 FAIL.zip,C:\1\new\33814018\ATstarMRDunderscoreMEP.xlsx"," , "
"33814028","CQ00259596","C:\1\new\33814028\ATstarRSTMemFullY.xlsx,C:\1\new\33814028\RSTMemFull_FAIL.zip,C:\1\new\33814028\RSTMemFull_PASS.zip"," , , "
"33814033","CQ00259601","C:\1\new\33814033\SIMDETEC_BUG.zip,C:\1\new\33814033\ATstarSIMDETEC.xlsx"," , "
"33814037","CQ00259605","C:\1\new\33814037\ATstarTGSINK.xlsx"," "
"33814048","CQ00259616","C:\1\new\33814048\BANSELCT.xlsx,C:\1\new\33814048\BANSELCT_TC1 20-03-14 15;13;15.zip,C:\1\new\33814048\BANSELCT_TC2 20-03-14 15;13;15.zip,C:\1\new\33814048\BANSELCT_TC3 20-03-14 15;13;15.zip,C:\1\new\33814048\BANSELCT_TC4 20-03-14 15;13;15.zip,C:\1\new\33814048\BANSELCT_TC5 20-03-14 15;13;15.zip"," , , , , , "
"33814049","CQ00259617","C:\1\new\33814049\AT+CACM.xlsx,C:\1\new\33814049\V14.1-RC2- CACM_TC5 FAIL.zip,C:\1\new\33814049\2014-02-17_ESHEL-e2_codeline1-CACM_TC5 FAIL.zip"," , , "
"33814050","CQ00259618","C:\1\new\33814050\AT+CAMM.xlsx"," "
"33814054","CQ00259622","C:\1\new\33814054\Charger AT commands system requirements.docx,C:\1\new\33814054\AT+CBC.xlsx,C:\1\new\33814054\CBC_TC4_BUG.icl"," , , "
"33814061","CQ00259629","C:\1\new\33814061\AT+CCFC.xlsx,C:\1\new\33814061\Log 15-Aug-13 09;00;45.icl,C:\1\new\33814061\CCFC-V10-Log 22-Oct-13 11;11;08.zip,C:\1\new\33814061\CCFC.zip"," , , , "
"33814062","CQ00259630","C:\1\new\33814062\AT+CCHC.xlsx"," "
"33814064","CQ00259632","C:\1\new\33814064\CCLK_Log 07-Nov-13 08;50;12.zip,C:\1\new\33814064\CCLK_quatationMark.zip,C:\1\new\33814064\CCLK_fail 6-1-14.zip,C:\1\new\33814064\CCLK_BUGS.zip"," , ,version: E2_NA04_0126_C1_DD, "
"33814065","CQ00259633","C:\1\new\33814065\AT+CCWA.xlsx,C:\1\new\33814065\CCWA_TC5_BUG.icl"," , "
"33814066","CQ00259634","C:\1\new\33814066\ts_127007v110500p_AT_commands.pdf","See 13.2.4 for System definition on AT+CDEFMP"
"33814070","CQ00259638","C:\1\new\33814070\ts_127007v110500p_AT_commands.pdf","see 13.2.2 for definition of AT+CDUP"

论坛徽章:
0
2 [报告]
发表于 2014-06-12 08:26 |只看该作者
大神们求助啊

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
3 [报告]
发表于 2014-06-12 08:26 |只看该作者
没看出有什么难度呀.

只要 把 逗号 到 zip 这一段(中间不包括 引号 逗号)删除就行了呀

论坛徽章:
0
4 [报告]
发表于 2014-06-12 09:29 |只看该作者
本帖最后由 sumin 于 2014-06-12 09:30 编辑

能不能给个示例啊?正则不是很熟悉的~

有些是包含多个zip文件的,我试了下总不太对,多谢了~

论坛徽章:
0
5 [报告]
发表于 2014-06-12 10:29 |只看该作者
用笨办法搞定了~~嘿嘿

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
6 [报告]
发表于 2014-06-12 11:02 |只看该作者
本帖最后由 huang6894 于 2014-06-12 11:37 编辑
  1. awk -F'"' '{gsub(/(,)?[^,]*.zip/,"",$6);print}' ufile
复制代码
同理:
  1. perl -ne '@a=split(/"/,$_);$a[5] =~ s/(,)?[^,]*zip//g;print join("\"",@a)'
复制代码

论坛徽章:
0
7 [报告]
发表于 2014-06-25 21:43 |只看该作者
本帖最后由 leo66888 于 2014-06-25 21:44 编辑

才学perl不久,希望能帮到你
结果是直接输出到屏幕(没重定向到一个文档)

#!perl -w
use strict;

my $fh;
my @a=();
my $txt_out;
my @b;

open $fh,'<','key-zip-removal.txt' or die "Couldn't open the file!!!\n";

while ( my $line = readline $fh ){
    chomp $line;
        push @a, $line;
}

foreach my $a(@a){
    @b = split /,/,$a;

        foreach my $b(@b){
            $b =~ s{C: \\ .* .zip}{}gx;
                $txt_out .="$b,";
        }
        @b = ();
       
        print $txt_out,"\n";
        print "\n";
        $txt_out = '';
}

__END__


下面是输出的结果,每行后面会多个"," 希望影响不大  :wink:

"dbid","display_name","Attachments","Description",

"33813948","CQ00259516","C:\1\new\33813948\CI PS Service Group.pdf,C:\1\new\3381
3948\ARME causes.xlsx,C:\1\new\33813948\CI MM Service Group.pdf,C:\1\new\3381394
8\CI CC Service Group.pdf,C:\1\new\33813948\ci_mm.h"," , , , ,ci_mm.h",

"33813950","CQ00259518","C:\1\new\33813950\AT$CCLK.xlsx,,,"," , , , ",

"33813951","CQ00259519","C:\1\new\33813951\CI MM Service Group.pdf,C:\1\new\3381
3951\ci_mm.h,C:\1\new\33813951\13340 51-at-t-spec.pdf,"," ,ci_mm.h ? interface f
ile updated,AT&T spec, ",

"33813952","CQ00259520","C:\1\new\33813952\ci_mm.h,","Updated ci_mm.h file, ",

"33813955","CQ00259523","C:\1\new\33813955\AT&F.xlsx,,,"," , , , ",

"33813956","CQ00259524",",C:\1\new\33813956\AT&S.xlsx"," , ",

"33813958","CQ00259526","C:\1\new\33813958\ATstarALS.xlsx,"," , ",

"33813962","CQ00259530","C:\1\new\33813962\PrefRat_PS441_2.7z,C:\1\new\33813962\
ci_dev.h,"," ,ci_dev.h, ",

"33813963","CQ00259531",""," ",

"33813976","CQ00259544",""," ",

"33813980","CQ00259548",",C:\1\new\33813980\ATstarCHECKEMER.xlsx"," , ",

"33813981","CQ00259549","C:\1\new\33813981\CIIND.docx","1",

"33813983","CQ00259551",""," ",

"33813984","CQ00259552",",C:\1\new\33813984\ATstarCNMA.xlsx"," , ",

"33813985","CQ00259553","C:\1\new\33813985\CNTI fail.txt,C:\1\new\33813985\ci_mm
.h,C:\1\new\33813985\CI MM Service Group.pdf,,C:\1\new\33813985\ATstarCNTI.xlsx"
," ,CI_MM,H, , , ",

"33813986","CQ00259554",",C:\1\new\33813986\ATstarCPBC.xlsx"," , ",

"33813990","CQ00259558",""," ",

"33813991","CQ00259559",",,C:\1\new\33813991\ATstarCSCB.xlsx,"," , , , ",

"33813993","CQ00259561","C:\1\new\33813993\ATstarCSIMSTAT.docx,C:\1\new\33813993
\CI SIM Service Group.pdf,,C:\1\new\33813993\asteriskCSIMSTAT.xlsx"," , , , ",

"33813994","CQ00259562",",C:\1\new\33813994\ATstarDEMERGNCY.xlsx"," , ",

"33814005","CQ00259573","C:\1\new\33814005\ATstarHSPACONFIG.docx,"," , ",

"33814006","CQ00259574",",,,C:\1\new\33814006\ATstarICCID.xlsx,,"," , , , ,ACAT
log from customer with failure - V9, ",

"33814013","CQ00259581","C:\1\new\33814013\MRD system016.pdf,C:\1\new\33814013\A
TstarMRDunderscoreAUTH.xlsx"," , ",

"33814017","CQ00259585","C:\1\new\33814017\MRD system016.pdf,C:\1\new\33814017\A
TstarMRDunderscoreIMEI.xlsx,,,"," , , , , ",

"33814018","CQ00259586",",C:\1\new\33814018\ATstarMRDunderscoreMEP.xlsx"," , ",

"33814028","CQ00259596","C:\1\new\33814028\ATstarRSTMemFullY.xlsx,,"," , , ",

"33814033","CQ00259601",",C:\1\new\33814033\ATstarSIMDETEC.xlsx"," , ",

"33814037","CQ00259605","C:\1\new\33814037\ATstarTGSINK.xlsx"," ",

"33814048","CQ00259616","C:\1\new\33814048\BANSELCT.xlsx,,,,,"," , , , , , ",

"33814049","CQ00259617","C:\1\new\33814049\AT+CACM.xlsx,,"," , , ",

"33814050","CQ00259618","C:\1\new\33814050\AT+CAMM.xlsx"," ",

"33814054","CQ00259622","C:\1\new\33814054\Charger AT commands system requiremen
ts.docx,C:\1\new\33814054\AT+CBC.xlsx,C:\1\new\33814054\CBC_TC4_BUG.icl"," , , "
,

"33814061","CQ00259629","C:\1\new\33814061\AT+CCFC.xlsx,C:\1\new\33814061\Log 15
-Aug-13 09;00;45.icl,,"," , , , ",

"33814062","CQ00259630","C:\1\new\33814062\AT+CCHC.xlsx"," ",

"33814064","CQ00259632",",,,"," , ,version: E2_NA04_0126_C1_DD, ",

"33814065","CQ00259633","C:\1\new\33814065\AT+CCWA.xlsx,C:\1\new\33814065\CCWA_T
C5_BUG.icl"," , ",

"33814066","CQ00259634","C:\1\new\33814066\ts_127007v110500p_AT_commands.pdf","S
ee 13.2.4 for System definition on AT+CDEFMP",

"33814070","CQ00259638","C:\1\new\33814070\ts_127007v110500p_AT_commands.pdf","s
ee 13.2.2 for definition of AT+CDUP",
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP