سؤال

I am trying to use grep to print only lines that start with a specific pattern. Here is an example

$SERVER_IP = 2.2.2.2
$SERVER_IP_PORT = 1111
$SERVER_IP_XXX = blablabla

I want grep to print only SERVER_IP = 2.2.2.2 and not the other three lines.

I tried the command below but it did not work

grep -e "^\s*\$SERVER_IP$"

If I try:

grep -e "^\s*\$SERVER_IP"

grep will print all three lines

How can I accomplish this using grep -e or egrep? Thank you

هل كانت مفيدة؟

المحلول

grep -e "^\s*\$SERVER_IP\>"

The \> means "word-boundary", or "place where word characters meet non-word characters."

نصائح أخرى

Use grep -e '^\$SERVER_IP =' to match any line that starts with $SERVER_IP =

If you have awk, you can do:

awk '$1=="$SERVER_IP"' file
$SERVER_IP = 2.2.2.2

The == makes it match only while field 1 is exact $SERVER_IP

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top