I think you want to add a leading prefix to your search string. If the word that you are trying to replace will always be preceded by a space:
sed -i 's/ VGHIER_TSV_WB798\./ "VGHIER_TSV_WB798"./g'
More generically, you can use the regex grouping [[:space:]]
to cover cases of leading spaces or tabs etc (see man isspace
for complete list).
sed -i 's/\([[:space:]]\)VGHIER_TSV_WB798\./\1"VGHIER_TSV_WB798"./g'
In that case, we needed to add an additional complexity by creating a regex grouping using the escaped parentheses, and use a back reference \1
to be sure not to lose the exact character when performing the replacement. There is also a regex class [[:punct:]]
for punctuation characters, in case the word is preceded sometimes by one of them. See man isspace
for a complete detailed list of what each regex character class covers.
Here's a more complex example solution to cover the case of possible punctuation prefixes:
sed -i 's/\([[:space:]]\|[[:punct:]]\)VGHIER_TSV_WB798\./\1"VGHIER_TSV_WB798"./g'
This makes use of the regex directive |
(escaped with a backslash for sed
) to indicate an alternative.
Finally, if the word will always be the first item on the line, use the ^
regex to require that:
sed -i 's/^VGHIER_TSV_WB798\./"VGHIER_TSV_WB798"./g'
And yes, for any of the above, you could have "group"ed and "back-referenced" your string:
sed -i 's/^\(VGHIER_TSV_WB798\)\./"\1"./g'
sed -i 's/\([[:space:]]\|[[:punct:]]\)\(VGHIER_TSV_WB798\)\./\1"\2"./g'
sed
are you using? – JigglyNaga Feb 08 '18 at 11:03