Search code examples
regexstringbashcut

Bash command to find and cut string


I am genereting txt information with tcpdump. 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".


Solution

  • You coud try something like grep -E "^Cookie:.*PHPSESSID.* id=[^ ]+$".

    It matches a line that

    • starts (^) with Cookie:
    • contains PHPSESSID (.*PHPSESSID.*)
    • and ends ($) with an id= field, due the [^ ] which prohibits spaces following the id=