Farhad Sakhaei
Farhad Sakhaei

Reputation: 922

grep lines contain exact string in a file

I have a file include many lines
I want to grep lines contain a string using a variable, which this string is a uri like:

/wp-admin/admin-ajax.php

I want to grep only lines contain exact uri:

/wp-admin/admin-ajax.php

and do not extract lines like this:

/wp-admin/admin-ajax.php?blahblah

Example File:

4.2.2.4 sitename.com/wp-admin/admin-ajax.php
4.2.2.4 sitename.com/wp-admin/admin-ajax.php?
4.2.2.4 sitename.com/wp-admin/admin-ajax.php?blah
4.2.2.4 sitename.com/wp-admin/admin-ajax.php?blahblah

Search subject:

sitename.com/wp-admin/admin-ajax.php

Should extract lines like this:

4.2.2.4 sitename.com/wp-admin/admin-ajax.php

Upvotes: 0

Views: 597

Answers (2)

Ed Morton
Ed Morton

Reputation: 203169

Add the -F option to grep to tell it to use strings instead of regexps and -x to mach whole lines:

grep -x -F "$variable"

See the man page.

Given your updated question:

$ awk -v tgt='sitename.com/wp-admin/admin-ajax.php' '$NF==tgt' file
4.2.2.4 sitename.com/wp-admin/admin-ajax.php

Upvotes: 1

Glrs
Glrs

Reputation: 1070

How about this:

awk 'NR==FNR{a[$0]; next} $1 in a' pattern.txt data_file.txt

Assuming that you keep your data in the data_file.txt like:

www.sitename.com/admin/index.php
www.sitename.com/admin
www.sitename.com/
...

And the stuff that you want to identify in another file, e.g. pattern.txt like:

www.sitename.com/admin

Upvotes: 0

Related Questions