3

次の問題があります。

私がしなければならないことは、別のファイルでこれらの単語を見つけて、それらを1文字だけに置き換えることです。

削除しなければならない単語がわからない(多すぎる!)ので、次のsedコマンドの使い方がわかりません

$ sed -i 's/words_old/word_new/g' /home/user/test.txt

ただし、 cat コマンドも使用する必要があると思います。

$ cat filewithwordstobedeleted.txt

しかし、それらを組み合わせる方法がわかりません。

助けてくれてありがとう!:)

ファビオ

4

3 に答える 3

3

ここでは、1 行に 1 つの単語を削除する必要があると仮定すると、単純なシェル ループが役立ちます。

cat filewithwordstobedeleted.txt | while read word; do
    sed -i "s/$word/null/g" /home/user/test.txt
done

の使用はcat厳密には必要ありませんが、この例を読みやすくすることに注意してください。

于 2012-06-22T13:54:37.983 に答える
0

これはあなたのために働くかもしれません(GNU sed):

# cat <<\! >/tmp/a
> this
> that
> those
> !
cat <<\! >/tmp/b
> a
> those
> b
> this
> c
> that
> d
> !
sed 's|.*|s/&/null/g|' /tmp/a
s/this/null/g
s/that/null/g
s/those/null/g
sed 's|.*|s/&/null/g|' /tmp/a | sed -f - /tmp/b
a
null
b
null
c
null
d
cat <<\! >/tmp/c
> a
> this and that and those
> b
> this and that
> c
> those
> !
sed 's|.*|s/&/null/g|' /tmp/a | sed -f - /tmp/c
a
null and null and null
b
null and null
c
null
于 2012-06-22T17:42:10.440 に答える
0

単語のリストが 1 行に 1 つずつあり、途方もなく長くない場合:

sed -ri "s/$(tr "\n" "|" < filewithwordstobedeleted.txt | head -c-1)/null/g" /home/user/test.txt
于 2012-06-22T14:58:39.670 に答える