2
I'd like to match all lines in a file that either my word is a prefix of, or the line is a prefix of my word. For example searching for "abc" should match:
a
ab
abc
abcd
abcxyz
but not:
xabc
zzab
xaz
The "my word is a prefix of" part is easy, just match on "^abc
" of course, but I haven't come up a solution for the "line is a prefix of my word" bit. Tried something in awk but wasn't able to make the line contents a part of the regular expression.
Perhaps you're looking for
grep -e '^abc' -e 'abc$'
– janos – 2017-02-02T20:33:35.697That won't match the prefixes of "
abc
", but will match lines for with it is a suffix, like "xabc
". – Ossifer – 2017-02-02T20:38:47.100egrep -E '^ab?c?'
? Give us an example what you want as result – Alex – 2017-02-02T20:45:49.133For that, you'd have to write
grep -E '^a$|^ab$|^abc'
– janos – 2017-02-02T20:52:35.4701
grep -e '^a' -e '^ab' -e '^abc'
? – Alex – 2017-02-02T20:56:15.060Alex and Janos: Yes, I can obviously enumerate the prefixes on my own, but this becomes tedious when the search string has many characters. I would also need to script it. – Ossifer – 2017-02-02T21:01:49.357
@Ossifer If you're scripting it, it's easy to create the regexp dynamically from the input, using a loop. – Barmar – 2017-02-02T21:34:41.620