Pensu
Pensu

Reputation: 3619

How to match a string with a space in the end to lines in a file using bash?

So, I am trying to match a string to some lines in a file. Now, the lines in the files are in the format:

peeyush (#$#@$)
peeyush-gupta ($%%&%)

Now, my problem is when I try to search 'peeyush', I should get only first line as the output. Till now, I have been doing this:

line='peeyush'
grep $line test.txt

Now, the search string would definitely come in a variable as this thing is going in loop. So, this gives me both the string as result. I want to match the string 'peeyush ' (with a space), while it's matching without spaces.

Upvotes: 0

Views: 51

Answers (2)

Zombo
Zombo

Reputation: 1

line='^peeyush '
grep "$line" test.txt

Upvotes: 0

SzG
SzG

Reputation: 12619

line=peeyush
grep "$line " test.txt

Upvotes: 4

Related Questions