免费注册 查看新帖 |

Chinaunix

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

[文本处理] 属性抽取 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-12-06 00:45 |只看该作者 |倒序浏览
本帖最后由 batty123292 于 2013-12-06 10:35 编辑

aa.word (990 Bytes, 下载次数: 19) bb.word (1.08 KB, 下载次数: 19)

现在有很多个文件,暂且提供两个,aa.word,bb.word。后续还会有cc.word,dd.word等等。
我想把其中标签为data-src的属性值抽出来,并且我只希望抽每个文件其中的前3个。
期望输出为一行,一共4个字段。第一个字段为文件的名字aa/bb/cc,第二个字段为第一个data-src对应的值,
第三个字段为第二个data-src对应的值,第四个字段为第三个data-src对应的值。

输出到一个文件里面,样例为:
aa  http://t1.gstatic.com/sh/sh/a  http://t0.gstatic.com  http://t3.gstatic.com/images?q
bb  httphsh.gstatic.com/sh/sj       httpnsklal://hs           http://t3mss/aj

aa.word为:
  1. aaaaaaaaaaaaaaaaaaaa
  2. aaaaaaaaaaaasasas
  3. target=_blank class=_l<img class=rg_i name=Veu1j2oKuKYzyM: data-src="http://t1.gstatic.com/sh/sh/a" data-sz=f onload="google.stb.csi.onTbn(1, this)"></a>gshjksk djlkl ksl oom=1" target=_blank class=rg_l<img class=rg_i name=4rq0eMtoD0OT6M: data-src="http://t0.gstatic.com" data-sz=f onload="google.stb.csi.onTbn(1, this)"></a>
  4. hskjaj dklasj dkla dkl
  5. target=_blank class=rg_l<img class=rg_i name=pM0PJS7WOnG-TM: data-src="http://t3.gstatic.com/images?q" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  6. sjk djlks dkls
  7. djkas djka
  8. gfwv4DoDA&amp;zoom=1" target=_blank class=rg_l<img class=rg_i name=fk2DM_qEo4qlcM: data-src="http://t3.gstatic.com/itbn:AN" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  9. sjk djkd kld kld
  10. =1" target=_blank class=rg_l<img class=rg_i name=FAisdPNr43SpYM: data-src="http://t0.gstatic.com/images?q" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  11. hsj dj dkl djks djk
  12. sjk djk djkl
  13. the end
复制代码
bb.word为:
  1. zh jsj dkl dkjld jk fkld
  2. target=_blank class=rg_l<img class=rg_i name=Veu1j2oKuKYzyM: data-src="httphsh.gstatic.com/sh/sj" data-sz=f onload="google.stb.csi.onTbn(1, this)"></a>
  3. gshjksk djlkl ksl
  4. oom=1" target=_blank class=rg_l<img class=rg_i name=4rq0eMtoD0OT6M: data-src="httpnsklal://hs" data-sz=f onload="google.stb.csi.onTbn(1, this)"></a>
  5. hskjaj dklasj dkla dkl
  6. target=_blank class=rg_l<img class=rg_i name=pM0PJS7WOnG-TM: data-src="http://t3mss/aj" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  7. sjk djlks dkls
  8. djkas djka
  9. gfwv4DoDA&amp;zoom=1" target=_blank class=rg_l<img class=rg_i name=fk2DM_qEo4qlcM: data-src="http://t3jsjka:AN" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  10. sjk djkd kld kld
  11. =1" target=_blank class=rg_l<img class=rg_i name=FAisdPNr43SpYM: data-src="http://t0.gstaticskaklsam/images?q" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  12. hsj dj dkl djks djk
  13. target=_blank class=rg_l<img class=rg_i name=FAisdPNr43SpYM: data-src="http://kslataticskaklsam/images?q" data-sz=f  onload="google.stb.csi.onTbn(1, this)"></a>
  14. hsj dj dkl djks djk
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2013-12-06 08:05 |只看该作者
回复 1# batty123292
  1. gawk4.0

  2. awk -vRS="data-src=" -F\" 'BEGINFILE{printf FILENAME}FNR<=4&&$2{printf OFS $2}ENDFILE{print ""}' file1 file2 ...
复制代码

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
3 [报告]
发表于 2013-12-06 09:06 |只看该作者
  1. awk '/data-src/{s=s"\t"gensub(/.*data-src="([^"]*).*/,"\\1",1);n++}n==3{s=FILENAME""s;sub(/\.[^\t]*/,"",s);print s;exit}' *.word
复制代码

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
4 [报告]
发表于 2013-12-06 09:20 |只看该作者
回复 2# yinyuemi
漏了个条件:前3个~

   

论坛徽章:
771
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
5 [报告]
发表于 2013-12-06 09:39 |只看该作者
  1. awk '{for(i=1;i<=NF;i++)if($i~/data-src/)src[++count]=gensub(/data-src=\"(.*)\"/,"\\1",1,$i);if(count==3){print "aa",src[1],src[2],src[3];exit}}' aa.word
复制代码
同时有个疑问,取出来的3个data-src字符串中,如果有两个值重复,
要去掉重复的在文件中取下一个吗?

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
6 [报告]
发表于 2013-12-06 10:17 |只看该作者
回复 4# yestreenstars


    我设了FNR<=4,应该没问题,帮我测试下,3ks

论坛徽章:
36
摩羯座
日期:2013-09-23 16:37:312015年亚洲杯之沙特阿拉伯
日期:2015-04-14 09:10:172015亚冠之柏太阳神
日期:2015-06-25 08:48:212015亚冠之武里南联
日期:2015-07-28 09:01:082015亚冠之莱赫维亚
日期:2015-07-28 15:44:172015亚冠之柏斯波利斯
日期:2015-09-06 14:08:52白银圣斗士
日期:2015-11-25 17:06:2815-16赛季CBA联赛之吉林
日期:2015-12-09 16:59:072016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之辽宁
日期:2016-04-14 09:29:04luobin
日期:2016-06-17 17:46:3615-16赛季CBA联赛之天津
日期:2016-08-16 14:11:01
7 [报告]
发表于 2013-12-06 10:19 |只看该作者
本帖最后由 LikeLx 于 2013-12-06 10:45 编辑
  1. awk '{for(i=0;i++<NF;) if($i~/data-src/&&a[data-src]++<3) s=s?s"\t"gensub(/data-src="([^"]*)"/,"\\1",1,$i):gensub(/data-src="([^"]*)"/,"\\1",1,$i)}END{print FILENAME,s}'
复制代码
没看清题意,代码只能对一个文件进行操作

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
8 [报告]
发表于 2013-12-06 10:35 |只看该作者
  1. awk -v RS='data-src="[^"]+"' 'FNR<=3{s=gensub(/[^"]+"([^"]+)"/,"\\1",1,RT);f=FILENAME;a[f]=a[f]?a[f]" "s:s}END{for(i in a)print i,a[i]}' file1 file2 file3 .....
复制代码

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
9 [报告]
发表于 2013-12-06 11:36 |只看该作者
  1. for i in $(ls *.word);do echo -n $i" ";grep -Pom3 '(?<=data-src=")[^"]+' $i|xargs;done
复制代码

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
10 [报告]
发表于 2013-12-06 12:05 |只看该作者
回复 9# dn833
  1. 1. for i in $(ls *.mp3)

  2. One of the most common mistakes BASH programmers make is to write a loop like this:

  3. for i in $(ls *.mp3); do    # Wrong!
  4.     some command $i          # Wrong!
  5. done

  6. for i in $(ls)              # Wrong!
  7. for i in `ls`               # Wrong!

  8. for i in $(find . -type f)  # Wrong!
  9. for i in `find . -type f`   # Wrong!
  10. Never use a CommandSubstitution -- of EITHER kind! -- around something that writes out filenames.

  11. Why? This breaks when a file has a space in its name. Why? Because the output of the $(ls *.mp3) command substitution undergoes WordSplitting. Assuming we have a file named 01 - Don't Eat the Yellow Snow.mp3 in the current directory, the for loop will iterate over each word in the resulting file name:

  12. some command 01
  13. some command -
  14. some command Don't
  15. some command Eat
  16. ...
  17. You can't double-quote the substitution either:

  18. for i in "$(ls *.mp3)"; do # Wrong!
  19. This causes the entire output of the ls command to be treated as a single word. Instead of iterating once for each file name, the loop will only execute once, with all the filenames rammed together.

  20. In addition to this, the use of ls is just plain unnecessary. It's an external command, which simply isn't needed to do the job. So, what's the right way to do it?

  21. for i in *.mp3; do  # Better! and...
  22.    some command "$i" # ...see Pitfall #2 for more info.
  23. done
  24. Let Bash expand the list of filenames for you. The expansion will not be subject to word splitting. Each filename that's matched by the *.mp3 glob will be treated as a separate word, and the loop will iterate once per filename.

  25. Question: What to do if there are no files *.mp3-files in the current directory? Then the for loop is executed once, with i="*.mp3", which is not the expected behaviour!

  26. Check the loop variable inside the loop:
  27. for i in *.mp3; do
  28.    [[ -f "$i" ]] || continue
  29.    some command "$i"
  30. done
  31. Reading lines of a file with a for loop is also wrong. Doubly (or possibly triply) so if those lines are filenames.

  32. Note the quotes around $i in the loop body. This leads to our second pitfall:
复制代码
@lkk_super http://bbs.chinaunix.net/thread-4113125-1-4.html  http://bash.cumulonim.biz/BashPitfalls.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP