Priya Danasegaran
Priya Danasegaran

Reputation: 1

Match exact string and returns only that line in UNIX

I'm looking to match only a particular string on a file and returns only that line. for eg, my matching pattern is /gp/digital/fiona/payment-checkout if I grep this one I'm getting all 4 lines as below,

/gp/digital/fiona/payment-checkout/img/logo.svg<br>
/gp/digital/fiona/payment-checkout/undefined<br>
/gp/digital/fiona/payment-checkout/uedata/nvp/unsticky<br>
/gp/digital/fiona/payment-checkout

But I want only the line which contains /gp/digital/fiona/payment-checkout. I tried grep -o -P -w everything.

Upvotes: 0

Views: 40

Answers (1)

James Brown
James Brown

Reputation: 37404

Using grep:

$ grep -x /gp/digital/fiona/payment-checkout file
/gp/digital/fiona/payment-checkout

man grep:

 -x, --line-regexp
              Select only those matches that exactly match the whole  line.   For  
              a  regular  expression  pattern,  this  is  like parenthesizing the 
              pattern and then surrounding it with ^ and $

Using awk:

$ awk '$0=="/gp/digital/fiona/payment-checkout"' file
/gp/digital/fiona/payment-checkout

Upvotes: 1

Related Questions