I'm trying to fix up some video transcripts on macOS, and I always have trouble matching "full words" (i.e. never within another word, like "raspy" for "spy") that are either surrounded by space, or occur at the beginning or end of a line. E.g.
"
01
spy flash does...
02
the spy flash does...
03
connect to spy
"
I have found individual pattern-matches as follows:
LC_ALL=C find . -type f -name '*.srt' -exec sed -i '' s/\^spy\ /\ SPI\ /g {}
LC_ALL=C find . -type f -name '*.srt' -exec sed -i '' s/\ spy\ /\ SPI\ /g {}
LC_ALL=C find . -type f -name '*.srt' -exec sed -i '' s/\ spy$/\ SPI/g {}
But I haven't been able to find a single-line pattern that matches all 3 cases. I know it should have something to do with there being zero or one (?) spaces on either side of the target word (spy, here) but all my attempts have come up empty. The reason I want a single pattern, is because I have to find lots of these sort of mistakes, and it is time-consuming to fill out each pattern in triplicate.
CodePudding user response:
I can't mark it as an answer since it's a comment, so just to put it, the syntax in the link https://stackoverflow.com/a/5734237/5440883 that Chris Maurer provided appears to work:
LC_ALL=C find . -type f -name '*.srt' -exec sed -i '' "s/[[:<:]]spy[[:>:]]/SPI/g" {}
CodePudding user response:
This would work in any sed that has a _E
argument to support EREs, e.g. GNU or BSD sed:
$ sed -E 's/(^| )spy( |$)/\1SPI\2/' file
"
01
SPI flash does...
02
the SPI flash does...
03
connect to SPI
"