Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to grep or sed a phrase that starts with =user / =Email and ends with &

Tags:

grep

bash

sed

I got .txt file with captured data which includes some usernames and passwords. One line f.e

user=testuser&password=somefunnyletters&cookie=true

I need to get just the phrase user=testuser without the rest. To get this line i've used grep ^user= mylogfile.txt. I've tried with

egrep -e "user=.*&" mylogfile.txt 

but got the same result.

I'm a newbie in the bash and grep. Could you help me? :)

EDIT: Thanks guys for answers Your solution with sed is even better :)

2016-01-19 10:34:53,707 POST Data (www.example.com):
user=testuser
2016-01-19 10:35:05,800 POST Data (accounts.google.com):
Page=SignIn
2016-01-19 10:35:11,118 SECURE POST Data (accounts.google.com):
Page=SignIn
2016-01-19 10:35:24,273 POST Data (mail.example.com):
username=ACCOUNT2

There's a pice of my log.txt

  2016-01-19 10:35:05,800 POST Data (accounts.google.com):
Page=SignIn&GALX=4sniYBF1KS4&gxf=AFoagUUWrsxLske465iHUiS7iLFTghUUVA%3A1453195970318&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&rm=false&ltmpl=default&scc=1&ss=1&osid=1&checkedDomains=youtube&checkConnection=&pstMsg=1&_utf8=%E2%98%83&bgresponse=%21eXpCtDFcUk0RqTZEOIof1FvHKm8CAAAAfVIAAAAPCgBHGejcPtA_v4HvVc7ixkq7wxLdz6ZvzEljoDpRfUwMD_czUNuCDpeVoBlAC77pd_nqcMFOMx6CkFDSBsZ1oCvv1M2h9WokdDkqANw3H-8BGAog77GOKPHJHR-FDUo5vutu5SKbkioAXvel_pcJOttUSp6glPAFQswWhastRHyyRn9axImbsoKlYsob3h604WklobLZNjQVbf9_cS1jbMQE-XWYBPE1FRXQzQysqKBLGHAmCUHvlaQfxYs1NsxAwmkNLoRBDAefVHTNT4YHqKEJ0LNkun8BnyxKT0jPTj7AebLpRUclWa9jF1FuVuakfi6ZbpqJnLK0XCFyQ8maiHvQh_SraectIJg_n4hBMkQ_riKZtRWPZCghzRS87Q3xezq2l-grUgO-&pstMsg=1&dnConn=&checkConnection=&checkedDomains=youtube&Email=EXAMPLEMAILUSER&Passwd=ewuoieuroiuio&signIn=Sign+in&PersistentCookie=yes&rmShown=1

Is there any possibility to get this

2016-01-19 10:35:05,800 POST Data (accounts.google.com):

and the Email=EXAMPLEMAILUSER ?

I need to get Email= , user= and username= . It would be nice to get for which site is username/email :)

It would help me a lot ;)

like image 205
uxez Avatar asked Feb 03 '26 21:02

uxez


1 Answers

grep -oP 'user=\K[^&]*'

or

grep -oP '^[^=]*=\K[^&]*' file

check this example:

kent$  echo "user=testuser&password=somefunnyletters&cookie=true"|grep -oP 'user=\K[^&]*'
testuser

kent$  echo "user=testuser&password=somefunnyletters&cookie=true"|grep -oP '^[^=]*=\K[^&]*'
testuser

Tested with grep:

kent$  grep --version|head -1
grep (GNU grep) 2.22
like image 181
Kent Avatar answered Feb 06 '26 13:02

Kent