I need to check whether a file contains an exact string, say string12345
, but not string123456
for example. This string can contain spaces! The file consists of various rows that all need to be checked.
But the string can be in a different place on the row, between dividing characters. The dividing characters can be either |
or ,
. Finally, the string can also be at the start or end of the row.
The check per row I have working so far is:
sLineFromFile=$(cat "${fFileToCheck}" | grep ^${sStringToCheck},)
So if sLineFromFile
is not empty, I know the file (fFileToCheck
) contains sStringToCheck
, but only at the start of the line ^
and when followed by a comma ,
.
I'm looking then for some kind of OR
feature like this:
sLineFromFile=$(cat "${fFileToCheck}" | grep [^,|]${sStringToCheck}[,|$])
I thought that this would work based on this Unix & Linux SE link but instead it appears to take the characters literally as written (so sLineFromFile
is always empty). I have tried many variations including:
sLineFromFile=$(cat "${fFileToCheck}" | grep @[^,|]${sStringToCheck},)
sLineFromFile=$(cat "${fFileToCheck}" | grep (^,|)${sStringToCheck},)
sLineFromFile=$(cat "${fFileToCheck}" | grep '[^,|]${sStringToCheck},')
sLineFromFile=$(cat "${fFileToCheck}" | grep [^,|]${sStringToCheck},)
(here just testing the character before sStringToCheck
).
How should I correct this?