Próbuję usunąć każdą instancję pewnejsłowo w pliku. Nie mogę tego zrobić, aby nie usuwał wzoru z innych słów. Na przykład, jeśli chcę usunąć słowo „the” z pliku. Usunie „the” z „then” i zostawi mnie z „n”.
W tej chwili próbowałem:
sed s/"$word"//g -i final_in
I:
sed "s/<"$word">//g" -i final_in
Ale żaden z nich nie zadziałał. Myślałem, że to będzie całkiem łatwe dla Google, ale każde znalezione przeze mnie rozwiązanie nie działa poprawnie.
Odpowiedzi:
1 dla odpowiedzi № 1$word="the"
$sed -r "s/b$wordb//g" << HEREDOC
> Sample text
> therefore
> then
> the sky is blue
> HEREDOC
Sample text
therefore
then
sky is blue
b = granica słowa
0 dla odpowiedzi nr 2
# test
word="the"
echo "aaa then bbb" | sed -r "s/$word//g"
# To match exacte word, you can add spaces :
word=then
echo "aaa then bbb" | sed -e "s/ $word / /g"
# to modify a file
word="the"
cat file.txt | sed -r "s/ $word / /g" > tmp.txt
mv tmp.txt file.txt
# to consider ponctuations :
word=then
echo "aaa. then, bbb" | sed -e "s/([:.,;/])* *$word *([:.,;/])*/1 2/g"