Board logo

标题: [文本处理] 批处理如何提取每行最后一个指定字符=之后(后面)的内容? [打印本页]

作者: 小智BAT    时间: 2016-3-6 21:53     标题: 批处理如何提取每行最后一个指定字符=之后(后面)的内容?

本帖最后由 pcl_test 于 2017-3-31 17:06 编辑

如题。提取内容如下:
__cfduid=de2f9d22740c61ba89977e825de4c23bc1457169473; gh_b633cdb54a50=o7fE2w5Pc-Fq5IjKgNjFoVl4bAjI
__cfduid=d17732cd680af4cf9607dfea23a1efe8e1457161975; gh_b633cdb54a50=o7fE2w20tghrUPTY_ydx9fdUwYKk


求大神提取o7fE2w5Pc-Fq5IjKgNjFoVl4bAjIo7fE2w20tghrUPTY_ydx9fdUwYKk内容。
作者: codegay    时间: 2016-3-6 22:37

本帖最后由 codegay 于 2016-3-7 03:49 编辑

python
  1. import re
  2. txt=open("a.txt").read()
  3. print(re.findall("""gh_[\w_-]+=([\w-]+)""",txt,re.DOTALL))
复制代码

作者: pcl_test    时间: 2016-3-6 22:51

  1. sed -n "s/.*=//p" a.txt
复制代码
  1. gawk -F"=" "{print $NF}" a.txt
复制代码

作者: codegay    时间: 2016-3-7 00:58

本帖最后由 codegay 于 2016-3-7 03:42 编辑

python
  1. [print(r.split("=")[2].rstrip("\n")) for r  in open("a.txt").readlines()]
复制代码

作者: codegay    时间: 2016-3-7 03:50

写成一行主要方便直接贴到IDLE中运行。
  1. import re;txt=open("a.txt").read();print(re.findall("""gh_[\w_-]+=([\w-]+)""",txt,re.DOTALL))
复制代码

作者: gawk    时间: 2016-3-7 14:17

  1. sed "s/.*=//" 1.txt > 2.txt
复制代码
  1. gawk -F"=" "$0=$NF" 1.txt > 2.txt
复制代码
  1. grep -Po "(?<==)[^=]*$" 1.txt > 2.txt
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2