nocturne
nocturne

Reputation: 647

Find lines with exact string bash

I need to find all lanes which contains exact string, for example we have this input:

/home/admin fileA /home/admin/bin fileB

and I need to find line

/home/admin fileA

I suppose I should use grep /home/admin but it don’t work the way I want to even with -w

Upvotes: 3

Views: 2108

Answers (3)

anubhava
anubhava

Reputation: 785098

Using awk:

awk '$1 == "/home/admin"' file
/home/admin fileA

EDIT: As per comment below you can use:

awk -v p=$(pwd) '$1 == p' file

Using grep:

grep -F '/home/admin ' file
/home/admin fileA

Upvotes: 4

glenn jackman
glenn jackman

Reputation: 246799

Search for your term followed by a space or the end of line

grep '/home/admin\($\|[[:blank:]]\)' file

or, a little tidier

grep -E '/home/admin($|[[:blank:]])' file

grep -w does not work because it wraps your search term in word boundary markers, and, for the 2nd line, there is a word boundary between admin and /

Upvotes: 1

hometoast
hometoast

Reputation: 11782

Grep would work if you want to find anything that starts with /home/admin but not /home/admin/.

grep '^/home/admin[^/]' file

the [^/] means "anything BUT a slash"

Upvotes: 1

Related Questions