简体   繁体   English

Bash命令查找并剪切字符串

[英]Bash command to find and cut string

I am genereting txt information with tcpdump. 我正在用tcpdump生成txt信息。 In output there is lots of information which contains something like that for instance: 在输出中,有很多信息,例如包含类似的信息:

Cookie: id=8xdza1ud39t459hv5bkth4t9gx71dicp; _ga=GA1.2.1890484279.1447252334; __gfp_64b=64smeVuOU7LSnfV8__mvHUkZFQF4lddYYY2X0c0HYAj.S7; __gads=ID=6ccc5cd620798f1a:T=1451575639:S=ALNI_MYeOVLFzU3qJTBxFq6KrdK10QDVrg; __utma=37331766.1890484279.1447252334.1451770396.1451770396.1; __utmz=37331766.1451770296.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); PHPSESSID=70c71a0c25d7d0425c657cee8f30dc2d; OX_sd=1; OX_plg=swf|shk|pm; _gat=1; _gat_newTracker=1; id=iyybhxg7524r2gcuvpguzvbse18c9c21

Is it possible to cut only this info from what i have and send it to another output? 是否可以仅从我拥有的信息中剪切此信息并将其发送到另一个输出? To match above cookie it needs to be a string which starts with "Cookie" ,ends with "id" atribute and contains "PHPSEESSID". 为了匹配上面的cookie,它必须是一个以“ Cookie”开头,以“ id”属性结尾并且包含“ PHPSEESSID”的字符串。

You coud try something like grep -E "^Cookie:.*PHPSESSID.* id=[^ ]+$" . 您可以尝试使用grep -E "^Cookie:.*PHPSESSID.* id=[^ ]+$"

It matches a line that 它匹配一条线

  • starts ( ^ ) with Cookie: 以Cookie开头( ^ ):
  • contains PHPSESSID ( .*PHPSESSID.* ) 包含PHPSESSID( .*PHPSESSID.*
  • and ends ( $ ) with an id= field, due the [^ ] which prohibits spaces following the id= 并以id=字段结尾( $ ),原因是[^ ]禁止在id=使用空格

使用grep命令和所需的正则表达式进行匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM