I have a file on the form
2e95d7582c53583fa8afb54e0fe7a2597c92cbba 1461065389 52880 temp/hello/file.txt
46c897a7aa8a641f46080b3431860bd0cd4a8f05 1461066221 207 temp/Another file.txt
83c8ce6b163ec1c615617fa0dbde9e928bc3daf4 1461056193 86112 Pictures/a photo.jpg
...
That is, each line has a 40-character long hex number, followed by space, an integer, space, an integer, space, and a file path that may contain spaces. Each path is unique.
In a bash script I have variables on the same form as a line in the file, only without the first hex string and space (41 first characters), e.g.:
myvar="1461066221 207 temp/Another file.txt"
My goal is to find the line in the file that matches myvar
exactly when the first 41 characters are ignored. If such a match is found I would like the variable line
to be set to the full line in the file. For the example above, line
would be set to
46c897a7aa8a641f46080b3431860bd0cd4a8f05 1461066221 207 temp/Another file.txt
If there is no such match, line
should be set to the empty string, or left unset.
My solution is this (filelist
is the name of file):
line=$(grep --color=never -E "^.{41}$myvar$" $filelist)
A problem is that when $myvar
is expanded it might contain special symbols like .
, +
, even ^
or $
, etc, which have a special meaning for grep
. I want grep
to perform an exact match of myvar
on all but the first 41 characters of a line.
grep
's-F
option. You just can't specify "at the end" with that.