mihir S
mihir S

Reputation: 647

Grep string in a file containing colon

I have following string in a multi-line file

.....<other lines>
-classpath "${ROOT}/abcjars/*:${ROOT}/lib/*" \
.....<otherlines>

I want to check if classpath line is present in file or not

If I try following grep command it works properly,

grep '\-classpath \"${ROOT}/abcjars/*' $File

But if I add colon (:) in search pattern it doesn't work

grep '\-classpath \"${ROOT}/abcjars/*:' $File

I want to grep and check if entire line is present or not. Is it possible?

Thanks

Upvotes: 0

Views: 2545

Answers (2)

Barmar
Barmar

Reputation: 782097

In a regular expressionm, * has special meaning, it means to match the preceding character zero or more times. But in your file you don't have : immediately after a sequence of / (because * is between them), so it's not matching.

You're not doing any regular expression pattern matching, so you should use the -F option to specify that this is a fixed string.

grep -F -e '-classpath "${ROOT}/abcjars/*:' $File

The -e option is needed to allow a search string that begins with -, otherwise the search string is treated as an option.

Upvotes: 2

Pradeep G
Pradeep G

Reputation: 31

Use escaping character

grep '\-classpath \"${ROOT}/abcjars/\*:' $File

If still same issue, try using grep regexp flag

Upvotes: 1

Related Questions