标签:xpehh test2 匹配 ee test jj linux 字符串 txt
001、shell实现
root@PC1:/home/test2# ls test.txt root@PC1:/home/test2# cat test.txt ## 测试数据, 删除第一次匹配xpehh以外的所有匹配xpehh的行 01 ee ff 02 ee de 03 dd ee 04 jj xpehh 05 jj kk 06 ee de 07 mm xpehh 08 ff ww 09 jj kk 10 mm xpehh 11 dd ee root@PC1:/home/test2# awk 'BEGIN{idx = 0} {if($0 ~ /xpehh/ && idx == 0 || $0 !~ /xpehh/) {print $0}; if($0 ~ /xpehh/) {idx++}}' test.txt 01 ee ff 02 ee de 03 dd ee 04 jj xpehh 05 jj kk 06 ee de 08 ff ww 09 jj kk 11 dd ee
002、python实现
root@PC1:/home/test2# ls test.py test.txt root@PC1:/home/test2# cat test.txt 01 ee ff 02 ee de 03 dd ee 04 jj xpehh 05 jj kk 06 ee de 07 mm xpehh 08 ff ww 09 jj kk 10 mm xpehh 11 dd ee root@PC1:/home/test2# cat test.py #!/usr/bin/python in_file = open("test.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() idx = 0 for i in lines: if idx == 0 and "xpehh" in i or "xpehh" not in i: out_file.write(i) if "xpehh" in i: idx = idx + 1 in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls result.txt test.py test.txt root@PC1:/home/test2# cat result.txt ## 结果文件 01 ee ff 02 ee de 03 dd ee 04 jj xpehh 05 jj kk 06 ee de 08 ff ww 09 jj kk 11 dd ee
003、R实现
dir() dat <- read.table("test.txt") result <- vector() idx = 0 for (i in 1:nrow(dat)) { if (sum(grepl("xpehh", dat[i,])) != 0 & idx == 0 | sum(grepl("xpehh", dat[i,])) == 0) { result <- c(result, i) } if(sum(grepl("xpehh", dat[i,])) != 0){ idx = idx + 1 } } final <- dat[result,] final
标签:xpehh,test2,匹配,ee,test,jj,linux,字符串,txt 来源: https://www.cnblogs.com/liujiaxin2018/p/16545714.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。